UserController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace client\controllers;
  3. use biz\merchant\services\MerchantExtendService;
  4. use biz\message\services\SmsService;
  5. use biz\user\classes\UserClass;
  6. use biz\user\services\UserAssetService;
  7. use biz\user\services\UserService;
  8. use common\components\stringUtil;
  9. use common\components\wxUtil;
  10. use Yii;
  11. use common\components\util;
  12. use yii\helpers\Json;
  13. class UserController extends BaseController
  14. {
  15. //获取登陆客户的资产 shish 2019.11.22
  16. public function actionAsset()
  17. {
  18. $asset = UserAssetService::getByUserId($this->userId);
  19. util::success($asset);
  20. }
  21. //获取登陆客户的优惠情况 shish 2019.12.3
  22. public function actionDiscount()
  23. {
  24. $discount = UserAssetService::getDiscount($this->userId);
  25. util::success($discount);
  26. }
  27. //小程序用户完整信息 shish 2019.12.12
  28. public function actionMiniFullInfo()
  29. {
  30. $post = Yii::$app->request->post();
  31. $iv = isset($post['iv']) ? $post['iv'] : '';
  32. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  33. $appId = $this->merchant['miniAppId'];
  34. $merchantId = $this->merchantId;
  35. $miniOpenId = $this->user['miniOpenId'];
  36. if (empty($miniOpenId)) {
  37. util::fail('mini_open_id empty');
  38. }
  39. $cacheKey = 'MINI_SESSION_KEY_' . $miniOpenId;
  40. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  41. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  42. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  43. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  44. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  45. if ($errCode != 0) {
  46. Yii::info($result . ' ' . $errCode);
  47. util::fail('获取用户信息失败');
  48. }
  49. Yii::info('小程序获取用户信息:' . $result);
  50. $originalInfo = Json::decode($result);
  51. $source = UserClass::$userSourceId['mini']['name'];
  52. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  53. $userId = $user['id'];
  54. $userInfo = UserService::getUserInfo($userId);
  55. util::success($userInfo);
  56. }
  57. //小程序用户手机号
  58. public function actionMiniMobile()
  59. {
  60. $post = Yii::$app->request->post();
  61. $iv = $post['iv'];
  62. $encryptedData = $post['encryptedData'];
  63. $merchantId = $this->merchantId;
  64. $merchant = $this->merchant;
  65. $appId = $merchant['miniAppId'];
  66. $miniOpenId = $this->user['miniOpenId'];
  67. $user = $this->user;
  68. if (!empty($user) && !empty($user['mobile'])) {
  69. $userId = $user['id'];
  70. UserAssetService::updateByUserId($userId, ['member' => 0]);
  71. util::success(['mobile' => $user['mobile']]);
  72. }
  73. $cacheKey = 'MINI_SESSION_KEY_' . $miniOpenId;
  74. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  75. if (empty($sessionKey)) {
  76. util::fail('sesstion key empty');
  77. }
  78. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  79. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  80. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  81. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  82. if ($errCode != 0) {
  83. util::fail('解密失败');
  84. }
  85. $arr = Json::decode($result);
  86. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  87. $userId = $user['id'];
  88. /**升级成为会员**/
  89. UserService::upgradeToMember($userId, $merchantId, ['mobile' => $mobile]);
  90. util::success(['mobile' => $mobile]);
  91. }
  92. //密码修改
  93. public function actionPassword()
  94. {
  95. $post = Yii::$app->request->post();
  96. $old = isset($post['old']) ? $post['old'] : '';
  97. $new = isset($post['new']) ? $post['new'] : '';
  98. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  99. if (empty($new)) {
  100. util::fail('请输入新密码');
  101. }
  102. if ($new != $confirm) {
  103. util::fail('二次密码不一致');
  104. }
  105. $user = $this->user;
  106. $userId = $this->userId;
  107. if (!empty($user['password'])) {
  108. if (empty($old)) {
  109. util::fail('请填写旧密码');
  110. }
  111. if (password_verify($old, $user['password']) == false) {
  112. util::fail('旧密码错误');
  113. }
  114. }
  115. UserService::updateById($userId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  116. util::ok('修改成功');
  117. }
  118. //申请会员 shish 2019.12.18
  119. public function actionApplyMember()
  120. {
  121. $post = Yii::$app->request->post();
  122. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  123. if (stringUtil::isMobile($mobile) == false) {
  124. util::fail('请输入手机号');
  125. }
  126. $userId = $this->userId;
  127. $asset = UserAssetService::getByUserId($userId);
  128. if (isset($asset['member']) && $asset['member'] != -1) {
  129. util::fail('已经是会员了');
  130. }
  131. $code = isset($post['code']) ? $post['code'] : '';
  132. $cacheCode = SmsService::getApplyMemberCode($userId);
  133. if (empty($cacheCode) || $cacheCode != $code) {
  134. util::fail('验证码错误');
  135. }
  136. $update = [];
  137. if (isset($post['realName']) && !empty($post['realName'])) {
  138. $update['realName'] = $post['realName'];
  139. }
  140. if (isset($post['birthday']) && !empty($post['birthday'])) {
  141. $update['birthday'] = $post['birthday'];
  142. }
  143. $user = UserService::getByCondition(['merchantId' => $this->merchantId, 'mobile' => $mobile]);
  144. if (!empty($user)) {
  145. util::fail('手机号已经注册过了');
  146. }
  147. $update['mobile'] = $mobile;
  148. UserService::upgradeToMember($userId, $this->merchantId, $update);
  149. util::ok('注册成功');
  150. }
  151. public function html_out($str)
  152. {
  153. if (function_exists('htmlspecialchars_decode')) {
  154. $str = htmlspecialchars_decode($str);
  155. } else {
  156. $str = html_entity_decode($str);
  157. }
  158. $str = stripslashes($str);
  159. return $str;
  160. }
  161. //用户基本信息和资产 shish 2019.12.19
  162. public function actionMyProfile()
  163. {
  164. $user = UserService::getUserInfo($this->userId);
  165. $content = '&lt;p&gt;&lt;a data-miniprogram-appid=&quot;wx3a163add860e0a86&quot; data-miniprogram-path=&quot;pages/case/casedetail?mainId=184&amp;owner_user_id=19&quot; href=&quot;&quot;&gt;&lt;img src=&quot;https://www.meijiabang.com/0.gif&quot; alt=&quot;&quot; data-width=&quot;null&quot; data-ratio=&quot;NaN&quot;&gt;&lt;/a&gt;&lt;/p&gt;';
  166. $content = $this->html_out($content);
  167. $r = wxUtil::sendMsg($content, $this->merchant, $user['openId']);
  168. Yii::info(json_encode($r));
  169. $growth = $user['userAsset']['growth'];
  170. $extend = $this->merchantExtend;
  171. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  172. //下个等级的积分
  173. $nextLevelGrowth = 0;
  174. foreach ($grade as $val) {
  175. $currentGrowth = $val['growth'];
  176. if ($currentGrowth > $growth) {
  177. $nextLevelGrowth = $currentGrowth;
  178. break;
  179. }
  180. }
  181. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  182. util::success($user);
  183. }
  184. }