sms_model.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. console.log('222');
  38. throw '请求错误';
  39. }
  40. if (postResponse.statusCode !== 200) {
  41. throw '通行证验证失败!';
  42. }
  43. resolve(body);
  44. });
  45. } catch (error) {
  46. reject([]);
  47. }
  48. });
  49. }
  50. /**
  51. * 根据手机号和短信调用SSO短信接口并更新手机号
  52. *
  53. * @param {string} ssoid
  54. * @param {string} mobile
  55. * @param {string} code
  56. * @return {int} 返回状态码
  57. */
  58. async checkCodeFromSSO(ssoid, mobile, code) {
  59. let postData = {
  60. url: 'http://sso.smartcost.com.cn/building/api/set/mobile',
  61. form: {ssoId: ssoid, mobile: mobile, code: code},
  62. encoding: 'utf8'
  63. };
  64. return new Promise(function (resolve, reject) {
  65. try {
  66. // 请求接口
  67. Request.post(postData, function (err, postResponse, body) {
  68. if (err) {
  69. console.log('333');
  70. throw '请求错误';
  71. }
  72. if (postResponse.statusCode !== 200) {
  73. throw '通行证验证失败!';
  74. }
  75. resolve(body);
  76. });
  77. } catch (error) {
  78. reject([]);
  79. }
  80. });
  81. }
  82. /**
  83. * 根据状态码获取反馈信息
  84. *
  85. * @param {string} status
  86. * @return {string}
  87. */
  88. async getStatusMsg(status) {
  89. return this.statusMsg[status];
  90. }
  91. }
  92. export default SmsModel;