sms_model.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * 短信业务模型
  3. *
  4. * @author EllisRan
  5. * @date 2018/4/17
  6. * @version
  7. */
  8. import Request from "request";
  9. import BaseModel from "../../common/base/base_model";
  10. class SmsModel extends BaseModel {
  11. /**
  12. * 状态信息
  13. *
  14. * @var {object}
  15. */
  16. statusMsg = { 1:'发送成功', 2:'该手机号已注册', 3:'该手机号未注册', 0:'参数有误',
  17. 4:'短信接口方出错', 5:'验证码添加到数据库出错', 6:'手机号更改到数据库出错',
  18. 7:'验证码有误', 8:'sso账号不存在' };
  19. /**
  20. * 根据手机号和短信类型调用SSO短信接口获取信息
  21. *
  22. * @param {string} mobile
  23. * @param {int} type 短信用途: 1->未注册过sso手机或更换手机号; 2->找回密码或修改密码;
  24. * @return {int} 返回状态码
  25. */
  26. async sendSmsFromSSO(mobile, type) {
  27. let postData = {
  28. url: 'http://sso.smartcost.com.cn/building/api/smscode',
  29. form: {mobile: mobile, type: type},
  30. encoding: 'utf8'
  31. };
  32. return new Promise(function (resolve, reject) {
  33. try {
  34. // 请求接口
  35. Request.post(postData, function (err, postResponse, body) {
  36. if (err) {
  37. throw '请求错误';
  38. }
  39. if (postResponse.statusCode !== 200) {
  40. throw '通行证验证失败!';
  41. }
  42. resolve(body);
  43. });
  44. } catch (error) {
  45. reject([]);
  46. }
  47. });
  48. }
  49. /**
  50. * 根据手机号和短信调用SSO短信接口并更新手机号
  51. *
  52. * @param {string} ssoid
  53. * @param {string} mobile
  54. * @param {string} code
  55. * @return {int} 返回状态码
  56. */
  57. async checkCodeFromSSO(ssoid, mobile, code) {
  58. let postData = {
  59. url: 'http://sso.smartcost.com.cn/building/api/set/mobile',
  60. form: {ssoId: ssoid, mobile: mobile, code: code},
  61. encoding: 'utf8'
  62. };
  63. return new Promise(function (resolve, reject) {
  64. try {
  65. // 请求接口
  66. Request.post(postData, function (err, postResponse, body) {
  67. if (err) {
  68. throw '请求错误';
  69. }
  70. if (postResponse.statusCode !== 200) {
  71. throw '通行证验证失败!';
  72. }
  73. resolve(body);
  74. });
  75. } catch (error) {
  76. reject([]);
  77. }
  78. });
  79. }
  80. /**
  81. * 根据状态码获取反馈信息
  82. *
  83. * @param {string} status
  84. * @return {string}
  85. */
  86. async getStatusMsg(status) {
  87. return this.statusMsg[status];
  88. }
  89. }
  90. export default SmsModel;