PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/api/mobile/api_v1_signs.rb

https://github.com/Feo/SZM15
Ruby | 379 lines | 342 code | 25 blank | 12 comment | 29 complexity | fa3437a8f56fa94bc45ba16d4802220d MD5 | raw file
  1. #encoding: utf-8
  2. require "helpers"
  3. require 'net/http'
  4. require 'digest/md5'
  5. module Mobile
  6. class API_v1_signs < Grape::API
  7. version 'v1', :using => :path, :vendor => 'sinoinfo', :format => :json
  8. resource :signs do
  9. before do
  10. authenticate!
  11. end
  12. post 'mobile_pw' do
  13. @account = Account.find_by_id(params[:id])
  14. if @account.nil?
  15. error!({"error" => "账户ID错误。" }, 400)
  16. end
  17. @token = @account.tokens.first
  18. #连接Token服务器注册手机
  19. token_header = "\x21\x01\x01sinoinfo00000001\x00\x1b"
  20. token_request_header = "\x00\x04\x00\x00\x02"
  21. token_item1 = "\x00\x00\x02\x0b" + @token.token_no
  22. token_item2 = "\x00\x01\x06\x03" + "999"
  23. #token报文格式
  24. token_packet = token_header + token_request_header + token_item1 + token_item2
  25. begin
  26. token_socket = TCPSocket.new '192.168.0.204', 8223
  27. rescue
  28. error!({"error" => "Token服务器异常。" }, 400)
  29. end
  30. token_socket.puts(token_packet)
  31. begin
  32. token_response = token_socket.read(36)
  33. rescue
  34. error!({"error" => "Token服务器异常。" }, 400)
  35. end
  36. token_socket.close
  37. content = "智票联激活码:" + token_response[30, 6]
  38. username = I18n.t('.smsbao.config.username')
  39. password = I18n.t('.smsbao.config.password')
  40. uri = URI('http://www.smsbao.com/sms')
  41. res = Net::HTTP.post_form(uri, 'u' => username, 'p' => password, 'm' => @token.token_no, 'c' => content )
  42. present "激活码发送成功。"
  43. end
  44. get 'all_signs' do
  45. if current_user.role == "主管"
  46. Sign.where("authorization IN (?) AND organization_id = ? AND deleted = ?", ["waiting", "refused", "manager_authorized"], current_user[:organization_id], false).paginate(:page => params[:page], :per_page => 5)
  47. else
  48. current_user.signs.where("authorization IN (?) AND organization_id = ? AND deleted = ?", ["waiting", "refused", "manager_authorized"], current_user[:organization_id], false).paginate(:page => params[:page], :per_page => 5)
  49. end
  50. end
  51. post 'previous_all_signs' do
  52. if current_user.role == "主管"
  53. Sign.where("authorization IN (?) AND organization_id = ? AND id < ? AND deleted = ?", ["waiting", "refused", "manager_authorized"], current_user[:organization_id], params[:sign_id], false).limit(5)
  54. else
  55. current_user.signs.where("authorization IN (?) AND organization_id = ? AND id < ? AND deleted = ?", ["waiting", "refused", "manager_authorized"], current_user[:organization_id], params[:sign_id], false).limit(5)
  56. end
  57. end
  58. get 'waiting_authorization' do
  59. current_user.signs.where("authorization = ? AND organization_id = ? AND deleted = ?", "waiting", current_user[:organization_id], false).paginate(:page => params[:page], :per_page => 5)
  60. end
  61. post 'previous_waiting_authorization' do
  62. current_user.signs.where("authorization = ? AND organization_id = ? AND id < ? AND deleted = ?", "waiting", current_user[:organization_id], params[:sign_id], false).limit(5)
  63. end
  64. get 'authorized_signs' do
  65. current_user.signs.where("authorization = ? AND organization_id = ? AND deleted = ?", "manager_authorized", current_user[:organization_id], false).paginate(:page => params[:page], :per_page => 5)
  66. end
  67. post 'previous_authorized_signs' do
  68. current_user.signs.where("authorization = ? AND organization_id = ? AND id < ? AND deleted = ?", "manager_authorized", current_user[:organization_id], params[:sign_id], false).limit(5)
  69. end
  70. get 'refused_signs' do
  71. current_user.signs.where("authorization = ? AND organization_id = ? AND deleted = ?", "refused", current_user[:organization_id], false).paginate(:page => params[:page], :per_page => 5)
  72. end
  73. post 'previous_refused_signs' do
  74. current_user.signs.where("authorization = ? AND organization_id = ? AND id < ? AND deleted = ?", "refused", current_user[:organization_id], params[:sign_id], false).limit(5)
  75. end
  76. get 'authorization_processing' do
  77. if current_user.role != "主管"
  78. error!({"error" => "只有主管才能执行该操作。" }, 400)
  79. end
  80. Sign.where("authorization = ? AND organization_id = ? AND deleted = ?", "waiting", current_user[:organization_id], false).paginate(:page => params[:page], :per_page => 5)
  81. end
  82. get ':id' do
  83. Sign.where("id = ? AND deleted = ?", params[:id], false).first
  84. end
  85. post 'delete' do
  86. @sign = current_user.signs.where("id = ? AND authorization IN (?) AND deleted = ?", params[:id], ["waiting", "refused", "manager_authorized"], false).first
  87. if @sign.nil?
  88. error!({"error" => "ID错误。" }, 400)
  89. else
  90. @sign.update_attribute("deleted", true)
  91. present @sign
  92. end
  93. end
  94. post 'authorize' do
  95. if current_user.role != "主管"
  96. error!({"error" => "只有主管才能执行该操作。" }, 400)
  97. end
  98. @sign = Sign.where("id = ? AND authorization = ? AND organization_id = ? AND deleted = ?", params[:id], "waiting", current_user[:organization_id], false).first
  99. if @sign.nil?
  100. error!({"error" => "ID错误。" }, 400)
  101. end
  102. @sign.update_attribute(:authorization, "manager_authorized")
  103. sendno = Time.now.to_i
  104. user = User.find_by_id(@sign.user_id)
  105. receiver_value = user.id.to_s
  106. input = sendno.to_s + I18n.t('.jpush.config.receiver_type').to_s + receiver_value.to_s + I18n.t('.jpush.config.master_secret').to_s
  107. md5 = Digest::MD5.hexdigest(input)
  108. send_description = "签发授权申请已批准。"
  109. n_content = "签发授权申请已批准。\n签发账号: #{@sign.account_no.slice(/\d+$/)}\n票据类型: #{@sign.bill_class.force_encoding("UTF-8")}\n签发日期: #{@sign.sign_date}\n收方帐号: #{@sign.receiver_account}\n支票号码: #{@sign.bill_no}\n签发金额: #{@sign.amount}"
  110. n_extras = Hash[:sign_id => @sign.id, :type => 3]
  111. msg_content = Hash[:n_content => n_content, :n_extras => n_extras].to_json
  112. output = Net::HTTP.post_form(URI.parse(I18n.t('.jpush.config.uri')),
  113. :sendno => sendno,
  114. :app_key => I18n.t('.jpush.config.app_key'),
  115. :receiver_type => I18n.t('.jpush.config.receiver_type'),
  116. :receiver_value => receiver_value,
  117. :verification_code => md5,
  118. :msg_type => I18n.t('.jpush.config.msg_type'),
  119. :msg_content => msg_content,
  120. :send_description => send_description,
  121. :time_to_live => I18n.t('.jpush.config.time_to_live'),
  122. :platform => I18n.t('.jpush.config.platform'))
  123. present @sign
  124. end
  125. post 'refuse_authorize' do
  126. if current_user.role != "主管"
  127. error!({"error" => "只有主管才能执行该操作。" }, 400)
  128. end
  129. @sign = Sign.where("id = ? AND authorization = ? AND organization_id = ?", params[:id], "waiting", current_user[:organization_id]).first
  130. if @sign.nil?
  131. error!({"error" => "ID错误。" }, 400)
  132. end
  133. @sign.update_attributes(:authorization => "refused", :reason => params[:reason])
  134. sendno = Time.now.to_i
  135. user = User.find_by_id(@sign.user_id)
  136. receiver_value = user.id.to_s
  137. input = sendno.to_s + I18n.t('.jpush.config.receiver_type').to_s + receiver_value.to_s + I18n.t('.jpush.config.master_secret').to_s
  138. md5 = Digest::MD5.hexdigest(input)
  139. send_description = "签发授权申请被拒绝。"
  140. n_content = "签发授权申请被拒绝。\n签发账号: #{@sign.account_no.slice(/\d+$/)}\n票据类型: #{@sign.bill_class.force_encoding("UTF-8")}\n签发日期: #{@sign.sign_date}\n收方帐号: #{@sign.receiver_account}\n支票号码: #{@sign.bill_no}\n签发金额: #{@sign.amount}"
  141. n_extras = Hash[:sign_id => @sign.id, :type => 4]
  142. msg_content = Hash[:n_content => n_content, :n_extras => n_extras].to_json
  143. output = Net::HTTP.post_form(URI.parse(I18n.t('.jpush.config.uri')),
  144. :sendno => sendno,
  145. :app_key => I18n.t('.jpush.config.app_key'),
  146. :receiver_type => I18n.t('.jpush.config.receiver_type'),
  147. :receiver_value => receiver_value,
  148. :verification_code => md5,
  149. :msg_type => I18n.t('.jpush.config.msg_type'),
  150. :msg_content => msg_content,
  151. :send_description => send_description,
  152. :time_to_live => I18n.t('.jpush.config.time_to_live'),
  153. :platform => I18n.t('.jpush.config.platform'))
  154. present @sign
  155. end
  156. post 'query_results' do
  157. if current_user.role == "主管" && params[:sign][:user_name].empty?
  158. if params[:sign][:date].empty?
  159. @signs = Sign.where("account_no like ?
  160. AND receiver_account like ?
  161. AND bill_class like ?
  162. AND bill_no like ?
  163. AND authorization like ?
  164. AND organization_id = ?",
  165. "%#{params[:sign][:account_no]}%",
  166. "%#{params[:sign][:receiver_account]}%",
  167. "%#{params[:sign][:bill_class]}%",
  168. "%#{params[:sign][:bill_no]}%",
  169. "signed",
  170. current_user[:organization_id]).paginate(:page => params[:page], :per_page => params[:per_page])
  171. present @signs
  172. else
  173. @signs = Sign.where("account_no like ?
  174. AND sign_date = ?
  175. AND receiver_account like ?
  176. AND bill_class like ?
  177. AND bill_no like ?
  178. AND authorization like ?
  179. AND organization_id = ?",
  180. "%#{params[:sign][:account_no]}%",
  181. params[:sign][:date],
  182. "%#{params[:sign][:receiver_account]}%",
  183. "%#{params[:sign][:bill_class]}%",
  184. "%#{params[:sign][:bill_no]}%",
  185. "signed",
  186. current_user[:organization_id]).paginate(:page => params[:page], :per_page => params[:per_page])
  187. present @signs
  188. end
  189. elsif current_user.role == "主管" && !params[:sign][:user_name].empty?
  190. @user = User.find_by_name(params[:sign][:user_name])
  191. if params[:sign][:date].empty?
  192. @signs = @user.signs.where("account_no like ?
  193. AND receiver_account like ?
  194. AND bill_class like ?
  195. AND bill_no like ?
  196. AND authorization like ?
  197. AND organization_id = ?",
  198. "%#{params[:sign][:account_no]}%",
  199. "%#{params[:sign][:receiver_account]}%",
  200. "%#{params[:sign][:bill_class]}%",
  201. "%#{params[:sign][:bill_no]}%",
  202. "signed",
  203. current_user[:organization_id]).paginate(:page => params[:page], :per_page => params[:per_page])
  204. present @signs
  205. else
  206. @signs = @user.signs.where("account_no like ?
  207. AND sign_date = ?
  208. AND receiver_account like ?
  209. AND bill_class like ?
  210. AND bill_no like ?
  211. AND authorization like ?
  212. AND organization_id = ?",
  213. "%#{params[:sign][:account_no]}%",
  214. params[:sign][:date],
  215. "%#{params[:sign][:receiver_account]}%",
  216. "%#{params[:sign][:bill_class]}%",
  217. "%#{params[:sign][:bill_no]}%",
  218. "signed",
  219. current_user[:organization_id]).paginate(:page => params[:page], :per_page => params[:per_page])
  220. present @signs
  221. end
  222. elsif current_user.role == "出纳员"
  223. if params[:sign][:date].empty?
  224. @signs = current_user.signs.where("account_no like ?
  225. AND receiver_account like ?
  226. AND bill_class like ?
  227. AND bill_no like ?
  228. AND authorization like ?
  229. AND organization_id = ?",
  230. "%#{params[:sign][:account_no]}%",
  231. "%#{params[:sign][:receiver_account]}%",
  232. "%#{params[:sign][:bill_class]}%",
  233. "%#{params[:sign][:bill_no]}%",
  234. "signed",
  235. current_user[:organization_id]).paginate(:page => params[:page], :per_page => params[:per_page])
  236. present @signs
  237. else
  238. @signs = current_user.signs.where("account_no like ?
  239. AND sign_date = ?
  240. AND receiver_account like ?
  241. AND bill_class like ?
  242. AND bill_no like ?
  243. AND authorization like ?
  244. AND organization_id = ?",
  245. "%#{params[:sign][:account_no]}%",
  246. params[:sign][:date],
  247. "%#{params[:sign][:receiver_account]}%",
  248. "%#{params[:sign][:bill_class]}%",
  249. "%#{params[:sign][:bill_no]}%",
  250. "signed",
  251. current_user[:organization_id]).paginate(:page => params[:page], :per_page => params[:per_page])
  252. present @signs
  253. end
  254. else
  255. error!({"error" => "操作权限不足。" }, 400)
  256. end
  257. end
  258. post 'issue_authorized_sign' do
  259. @sign = current_user.signs.where("id = ? AND authorization = ? AND organization_id = ?", params[:id], "manager_authorized", current_user[:organization_id]).first
  260. if @sign.nil?
  261. error!({"error" => "ID错误。" }, 400)
  262. elsif params[:sign][:token_pw].empty?
  263. error!({"error" => "请先认证Token口令。" }, 400)
  264. end
  265. @account = Account.find_by_account_no(@sign[:account_no].slice(/\d+$/))
  266. #连接token服务器认证动态口令
  267. @token = @account.tokens.find_by_user_id(current_user[:id])
  268. #token报头
  269. token_header = "\x21\x01\x01sinoinfo00000001\x00\x1c"
  270. #token请求头
  271. token_request_header = "\x00\x01\x00\x00\x02"
  272. #token数据体
  273. token_item1 = "\x00\x00\x02\x09" + "#{@token[:token_no]}"
  274. token_item2 = "\x00\x00\x03\x06" + "#{params[:sign][:token_pw]}"
  275. #token报文格式
  276. token_packet = token_header + token_request_header + token_item1 + token_item2
  277. begin
  278. token_socket = TCPSocket.new I18n.t('.tcp_config.config.token'), 8223
  279. rescue
  280. error!({"error" => "Token服务器异常。" }, 400)
  281. end
  282. token_socket.puts(token_packet)
  283. token_response = token_socket.read(26)
  284. token_socket.close
  285. #验证token服务器口令认证返回码
  286. if token_response[23, 2].unpack("C1C1").join("") != "01"
  287. error!({"error" => "Token口令认证失败。" }, 400)
  288. end
  289. case @sign[:bill_class]
  290. when "支票"
  291. bill_class = 1
  292. when "汇票申请书"
  293. bill_class = 2
  294. when "本票申请书"
  295. bill_class = 3
  296. when "汇总凭证"
  297. bill_class = 4
  298. when "其他票据"
  299. bill_class = 5
  300. end
  301. #组成生成支付密码请求数据包
  302. sign_header = ["125", @account.bank_no, "2001"].pack("A6A12A4")
  303. sign_body = ["#{@sign[:account_no].slice(/\d+$/)}",
  304. "00",
  305. " ",
  306. "#{@sign[:sign_date].to_s.delete("-")}",
  307. "#{bill_class}",
  308. "#{@sign[:amount]}",
  309. "#{@sign[:bill_no]}",
  310. "#{@sign[:receiver_account]}"].pack("A32A2A10A8A1A16A8A32")
  311. sign_packet = sign_header + sign_body
  312. #连接签发服务器生成支付密码
  313. begin
  314. sign_socket = TCPSocket.new I18n.t('.tcp_config.config.sign'), 8213
  315. rescue
  316. error!({"error" => "签发服务器异常。" }, 400)
  317. end
  318. sign_socket.puts(sign_packet)
  319. sign_response = sign_socket.read(28)
  320. sign_socket.close
  321. #验证签发服务器生成支付密码返回码
  322. if sign_response[6, 6] == "000000" && @sign.save
  323. @account.update_attribute(:amount, @account.amount.to_i + @sign.amount.to_i)
  324. @sign.update_attributes(:authorization => "signed",
  325. :pay_code => sign_response[12, 16])
  326. @account.signs << @sign
  327. sendno = Time.now.to_i
  328. manager = User.where("organization_id = ? AND role = ?", current_user[:organization_id], "主管").first
  329. receiver_value = manager.id.to_s
  330. input = sendno.to_s + I18n.t('.jpush.config.receiver_type').to_s + receiver_value.to_s + I18n.t('.jpush.config.master_secret').to_s
  331. md5 = Digest::MD5.hexdigest(input)
  332. send_description = "出纳员签发已授权签发申请。"
  333. n_content = "签发账号: #{@sign.account_no.slice(/\d+$/)}\n票据类型: #{@sign.bill_class.force_encoding("UTF-8")}\n签发日期: #{@sign.sign_date}\n收方帐号: #{@sign.receiver_account}\n支票号码: #{@sign.bill_no}\n签发金额: #{@sign.amount}\n支付密码: #{@sign.pay_code}"
  334. n_extras = Hash[:sign_id => @sign.id, :type => 6]
  335. msg_content = Hash[:n_content => n_content, :n_extras => n_extras].to_json
  336. output = Net::HTTP.post_form(URI.parse(I18n.t('.jpush.config.uri')),
  337. :sendno => sendno,
  338. :app_key => I18n.t('.jpush.config.app_key'),
  339. :receiver_type => I18n.t('.jpush.config.receiver_type'),
  340. :receiver_value => receiver_value,
  341. :verification_code => md5,
  342. :msg_type => I18n.t('.jpush.config.msg_type'),
  343. :msg_content => msg_content,
  344. :send_description => send_description,
  345. :time_to_live => I18n.t('.jpush.config.time_to_live'),
  346. :platform => I18n.t('.jpush.config.platform'))
  347. present @sign
  348. else
  349. error!({"error" => "签发失败。" }, 400)
  350. end
  351. end
  352. end
  353. end
  354. end