Sms.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\system\sms;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\config\ConfigValueRepository;
  14. use app\common\repositories\system\sms\SmsRecordRepository;
  15. use app\validate\admin\SmsRegisterValidate;
  16. use crmeb\services\YunxinSmsService;
  17. use think\App;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. * Class Sms
  23. * @package app\controller\admin\system\sms
  24. * @author xaboy
  25. * @day 2020-05-18
  26. */
  27. class Sms extends BaseController
  28. {
  29. /**
  30. * @var YunxinSmsService
  31. */
  32. protected $service;
  33. /**
  34. * Sms constructor.
  35. * @param App $app
  36. */
  37. public function __construct(App $app)
  38. {
  39. parent::__construct($app);
  40. $this->service = YunxinSmsService::create();
  41. }
  42. /**
  43. * @return mixed
  44. * @author xaboy
  45. * @day 2020-05-18
  46. */
  47. public function captcha()
  48. {
  49. $phone = request()->param('phone');
  50. if (!$phone)
  51. return app('json')->fail('请输入手机号');
  52. if (!preg_match('/^1[3456789]{1}\d{9}$/', $phone))
  53. return app('json')->fail('请输入正确的手机号');
  54. $res = $this->service->captcha($phone);
  55. if (!isset($res['status']) && $res['status'] !== 200)
  56. return app('json')->fail($res['data']['message'] ?? $res['msg'] ?? '发送失败');
  57. return app('json')->success($res['data']['message'] ?? $res['msg'] ?? '发送成功');
  58. }
  59. /**
  60. * @param SmsRegisterValidate $validate
  61. * @return mixed
  62. * @author xaboy
  63. * @day 2020-05-18
  64. */
  65. public function save(SmsRegisterValidate $validate)
  66. {
  67. $data = $this->request->params(['account', 'password', 'phone', 'code', 'url', 'sign']);
  68. $validate->check($data);
  69. $data['password'] = md5($data['password']);
  70. $res = $this->service->registerData($data);
  71. if ($res['status'] == 400) return app('json')->fail('短信平台:' . $res['msg']);
  72. $this->service->setConfig($data['account'], $data['password']);
  73. return app('json')->success('短信平台:' . $res['msg']);
  74. }
  75. /**
  76. * @param SmsRegisterValidate $validate
  77. * @param ConfigValueRepository $repository
  78. * @return mixed
  79. * @author xaboy
  80. * @day 2020-05-18
  81. */
  82. public function save_basics(SmsRegisterValidate $validate, ConfigValueRepository $repository)
  83. {
  84. $data = $this->request->params([
  85. 'account', 'password'
  86. ]);
  87. $validate->isLogin()->check($data);
  88. $this->service->setConfig($data['account'], md5($data['password']));
  89. //添加公共短信模板
  90. $templateList = $this->service->publictemp([]);
  91. if ($templateList['status'] != 400) {
  92. $repository->setFormData(['sms_account' => $data['account'], 'sms_token' => md5($data['password'])], 0);
  93. return app('json')->success('登录成功');
  94. } else {
  95. return app('json')->fail('账号或密码错误');
  96. }
  97. }
  98. /**
  99. * @return mixed
  100. * @author xaboy
  101. * @day 2020-05-18
  102. */
  103. public function is_login()
  104. {
  105. if ($sms_info = $this->service->account()) {
  106. return app('json')->success(['status' => true, 'info' => $sms_info]);
  107. } else {
  108. return app('json')->success(['status' => false]);
  109. }
  110. }
  111. /**
  112. * @param SmsRecordRepository $repository
  113. * @return mixed
  114. * @throws DataNotFoundException
  115. * @throws DbException
  116. * @throws ModelNotFoundException
  117. * @author xaboy
  118. * @day 2020-05-18
  119. */
  120. public function record(SmsRecordRepository $repository)
  121. {
  122. [$page, $limit] = $this->getPage();
  123. $where = $this->request->params(['type']);
  124. return app('json')->success($repository->getList($where, $page, $limit));
  125. }
  126. /**
  127. * @param SmsRecordRepository $repository
  128. * @return mixed
  129. * @author xaboy
  130. * @day 2020-05-18
  131. */
  132. public function data(SmsRecordRepository $repository)
  133. {
  134. $countInfo = $this->service->count();
  135. if ($countInfo['status'] == 400) {
  136. $info['number'] = 0;
  137. $info['total_number'] = 0;
  138. } else {
  139. $info['number'] = $countInfo['data']['number'];
  140. $info['total_number'] = $countInfo['data']['send_total'];
  141. }
  142. $info['record_number'] = $repository->count();
  143. $info['sms_account'] = $this->service->account();
  144. return app('json')->success($info);
  145. }
  146. /**
  147. * @param ConfigValueRepository $repository
  148. * @return mixed
  149. * @throws DbException
  150. * @author xaboy
  151. * @day 2020-05-18
  152. */
  153. public function logout(ConfigValueRepository $repository)
  154. {
  155. $repository->clear(['sms_account', 'sms_token'], 0);
  156. return app('json')->success('退出成功');
  157. }
  158. /**
  159. * 修改密码
  160. * @Author:Qinii
  161. * @Date: 2020/9/2
  162. * @return mixed
  163. */
  164. public function changePassword()
  165. {
  166. $data = $this->request->params(['password','phone','code']);
  167. if(empty($data['password']))
  168. return app('json')->fail('密码不能为空');
  169. $data['password'] = md5($data['password']);
  170. $res = $this->service->smsChange($data);
  171. if ($res['status'] == 400) return app('json')->fail('短信平台:' . $res['msg']);
  172. $this->service->setConfig($this->service->account(), $data['password']);
  173. return app('json')->success('修改成功');
  174. }
  175. /**
  176. * 修改签名
  177. * @Author:Qinii
  178. * @Date: 2020/9/2
  179. * @return mixed
  180. */
  181. public function changeSign()
  182. {
  183. $data = $this->request->params(['sign','phone','code']);
  184. if(empty($data['sign'])) return app('json')->fail('签名不能为空');
  185. $res = $this->service->smsChange($data);
  186. if ($res['status'] == 400) return app('json')->fail('短信平台:' . $res['msg']);
  187. return app('json')->success('修改已提交,审核通过后自动更改');
  188. }
  189. }