Center.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\store\controller\api\member;
  14. use app\store\controller\api\Member;
  15. use app\store\service\ExtendService;
  16. use think\Db;
  17. /**
  18. * 商品会员中心
  19. * Class Center
  20. * @package app\store\controller\api\member
  21. */
  22. class Center extends Member
  23. {
  24. /**
  25. * 修改会员资料
  26. * @throws \think\Exception
  27. * @throws \think\exception\PDOException
  28. */
  29. public function info()
  30. {
  31. $data = [];
  32. if ($this->request->has('headimg', 'post', true)) {
  33. $data['headimg'] = $this->request->post('headimg');
  34. }
  35. if ($this->request->has('nickname', 'post', true)) {
  36. $data['nickname'] = emoji_encode($this->request->post('nickname'));
  37. }
  38. if ($this->request->has('username', 'post', true)) {
  39. $data['username'] = emoji_encode($this->request->post('username'));
  40. }
  41. if (empty($data)) $this->error('没有需要修改的数据哦!');
  42. if (data_save('StoreMember', array_merge($data, ['id' => $this->mid]), 'id')) {
  43. $this->success('会员资料更新成功!', $this->getMember());
  44. }
  45. $this->error('会员资料更新失败,请稍候再试!');
  46. }
  47. /**
  48. * 发送短信验证码
  49. * @throws \think\Exception
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. * @throws \think\exception\DbException
  53. * @throws \think\exception\PDOException
  54. */
  55. public function sendsms()
  56. {
  57. $phone = $this->request->post('phone');
  58. if ($this->request->post('secure') !== sysconf('sms_secure')) {
  59. $this->error('短信发送安全码不正确,请使用正确的安全码!');
  60. }
  61. $member = Db::name('StoreMember')->where(['phone' => $phone])->find();
  62. if (!empty($member)) $this->error('该手机号已经注册了,请使用其它手机号!');
  63. $cache = cache($cachekey = "send_register_sms_{$phone}");
  64. if (is_array($cache) && isset($cache['time']) && $cache['time'] > time() - 120) {
  65. $dtime = ($cache['time'] + 120 < time()) ? 0 : (120 - time() + $cache['time']);
  66. $this->success('短信验证码已经发送!', ['time' => $dtime]);
  67. }
  68. list($code, $content) = [rand(1000, 9999), sysconf('sms_reg_template')];
  69. if (empty($content) || stripos($content, '{code}') === false) {
  70. $content = '您的验证码为{code},请在十分钟内完成操作!';
  71. }
  72. cache($cachekey, ['phone' => $phone, 'captcha' => $code, 'time' => time()], 600);
  73. if (empty($content) || strpos($content, '{code}') === false) {
  74. $this->error('获取短信模板失败,联系管理员配置!');
  75. }
  76. $cache = cache($cachekey);
  77. if (ExtendService::sendSms($this->mid, $phone, str_replace('{code}', $code, $content))) {
  78. $dtime = ($cache['time'] + 120 < time()) ? 0 : (120 - time() + $cache['time']);
  79. $this->success('短信验证码发送成功!', ['time' => $dtime]);
  80. }
  81. $this->error('短信发送失败,请稍候再试!');
  82. }
  83. /**
  84. * 会员登录绑定
  85. * @throws \think\Exception
  86. * @throws \think\exception\PDOException
  87. */
  88. public function bind()
  89. {
  90. $code = $this->request->post('code');
  91. $phone = $this->request->post('phone');
  92. $cache = cache($cachekey = "send_register_sms_{$phone}");
  93. if (is_array($cache) && isset($cache['captcha']) && $cache['captcha'] == $code) {
  94. $where = ['id' => $this->member['id']];
  95. if (Db::name('StoreMember')->where($where)->update(['phone' => $phone]) !== false) {
  96. $this->success('手机绑定登录成功!');
  97. }
  98. } else $this->error('短信验证码验证失败!');
  99. $this->error('手机绑定登录失败,请稍候再试!');
  100. }
  101. /**
  102. * 获取会员资源成功
  103. */
  104. public function member()
  105. {
  106. $this->success('获取会员资料成功!', $this->member);
  107. }
  108. }