UserController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\admin\services\AdminService;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\custom\services\CustomService;
  6. use biz\sj\classes\MerchantExtendClass;
  7. use biz\sj\services\MerchantAssetService;
  8. use biz\sj\services\MerchantRemarkService;
  9. use bizHd\user\services\UserAssetService;
  10. use bizHd\user\services\UserGrowthService;
  11. use bizHd\user\services\UserService;
  12. use bizHd\user\services\UserUpgradeService;
  13. use common\components\wxUtil;
  14. use common\services\xhMerchantExtendService;
  15. use common\services\xhRechargeService;
  16. use common\services\xhTMessageService;
  17. use common\services\xhUserIntegralService;
  18. use Yii;
  19. use common\components\stringUtil;
  20. use common\components\util;
  21. use bizHd\message\services\SmsService;
  22. use bizHd\user\classes\UserClass;
  23. use yii\helpers\Json;
  24. class UserController extends BaseController
  25. {
  26. public $guestAccess = ['my-profile', 'discount'];
  27. //客户列表 ssh 2019.11.30
  28. public function actionList()
  29. {
  30. $get = Yii::$app->request->get();
  31. $search = isset($get['search']) ? $get['search'] : '';
  32. $where = ['sjId' => $this->sjId];
  33. if (!empty($search)) {
  34. if (stringUtil::isMobile($search)) {
  35. $where['mobile'] = $search;
  36. } else {
  37. $where['name'] = ['like', $search];
  38. }
  39. }
  40. $list = CustomService::getCustomList($where);
  41. util::success($list);
  42. }
  43. //获取客户基本和资产信息 ssh 2019.11.30
  44. public function actionDetail()
  45. {
  46. $userId = Yii::$app->request->get('userId');
  47. $info = UserService::getUserInfo($userId);
  48. if (isset($info['sjId']) == false || $this->sjId != $info['sjId']) {
  49. util::fail('没有权限');
  50. }
  51. util::success($info);
  52. }
  53. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  54. public function actionUserDetail()
  55. {
  56. $info = [
  57. 'id' => '12545132',
  58. 'mobile' => '15280215347',
  59. 'subscribe' => 1,
  60. 'sex' => 0,
  61. 'userName' => '慈善的人',
  62. 'address' => '',
  63. 'visitTimeFormat' => "今天 10:57",
  64. 'sourceType' => 0,
  65. 'userAsset' => [
  66. 'balance' => 0.00,
  67. 'totalBuyNum' => 1,
  68. 'totalExpend' => 3,
  69. 'growth' => 30,
  70. 'getCouponNum' => 3,
  71. 'inviteNum' => 1,
  72. 'discount' => 1.00
  73. ],
  74. 'remarkList' => [],
  75. 'orderList' => [
  76. [
  77. 'id' => 215360,
  78. 'createTime' => '2021-01-31 10:58:13',
  79. 'actPrice' => 20.9,
  80. 'payWay' => 0,
  81. 'sendNum' => '79',
  82. 'status' => 5,
  83. ]
  84. ],
  85. ];
  86. util::success($info);
  87. $userId = Yii::$app->request->get('userId');
  88. $info = UserService::getFullUserInfo($userId);
  89. UserService::valid($info, $this->sjId);
  90. $list = MerchantRemarkService::getRemarkList($userId);
  91. $info['remarkList'] = $list;
  92. util::success($info);
  93. }
  94. //添加客户,申请会员时会自动同步资产 ssh 2019.11.30
  95. public function actionAdd()
  96. {
  97. $post = Yii::$app->request->post();
  98. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  99. util::fail('请输入手机号');
  100. }
  101. if ($post['mobile'] != $post['confirmMobile']) {
  102. util::fail('二次输入手机号不一样');
  103. }
  104. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  105. AdminService::verifyPayPassword($this->adminId, $payPassword);
  106. $mobile = $post['mobile'];
  107. $user = UserService::getByMobile($mobile, $this->sjId);
  108. if (!empty($user)) {
  109. util::fail('手机号已使用');
  110. }
  111. $post['sjId'] = $this->sjId;
  112. $post['member'] = $post['memberLevel'];
  113. UserService::addUser($post);
  114. util::complete();
  115. }
  116. //重置客户密码
  117. public function actionResetPayPassword()
  118. {
  119. $post = Yii::$app->request->post();
  120. $userId = isset($post['userId']) ? $post['userId'] : 0;
  121. $password = isset($post['password']) ? $post['password'] : 0;
  122. $info = UserService::getById($userId);
  123. UserService::valid($info, $this->sjId);
  124. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  125. util::complete();
  126. }
  127. //获取登陆客户的资产 ssh 2019.11.22
  128. public function actionAsset()
  129. {
  130. $asset = UserAssetService::getByUserId($this->adminId);
  131. util::success($asset);
  132. }
  133. //客户获取自己的优惠情况 ssh 2021.2.1
  134. public function actionDiscount()
  135. {
  136. $get = Yii::$app->request->get();
  137. $customId = $get['customId'] ?? 0;
  138. if (empty($customId)) {
  139. $customId = $this->customId;
  140. }
  141. if (empty($customId)) {
  142. util::fail('没有找到客户信息');
  143. }
  144. $info = CustomClass::getCustomInfo($customId);
  145. CustomClass::valid($info, $this->shopId);
  146. $level = $info['member'] ?? 0;
  147. $discount = $info['discount'] ?? 1;
  148. $discount = [
  149. 'member' => ['level' => $level, 'discount' => $discount],
  150. 'coupon' => [],
  151. ];
  152. util::success($discount);
  153. }
  154. //小程序用户完整信息 ssh 2019.12.12
  155. public function actionMiniFullInfo()
  156. {
  157. $post = Yii::$app->request->post();
  158. $iv = isset($post['iv']) ? $post['iv'] : '';
  159. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  160. $appId = $this->sj['miniAppId'];
  161. $sjId = $this->sjId;
  162. $miniOpenId = $this->user['miniOpenId'];
  163. if (empty($miniOpenId)) {
  164. util::fail('mini_open_id empty');
  165. }
  166. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  167. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  168. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  169. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  170. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  171. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  172. if ($errCode != 0) {
  173. Yii::info($result . ' ' . $errCode);
  174. util::fail('获取用户信息失败');
  175. }
  176. Yii::info('小程序获取用户信息:' . $result);
  177. $originalInfo = Json::decode($result);
  178. $source = UserClass::$userSourceId['mini']['name'];
  179. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  180. $userId = $user['id'];
  181. $userInfo = UserService::getUserInfo($userId);
  182. util::success($userInfo);
  183. }
  184. //小程序用户手机号
  185. public function actionMiniMobile()
  186. {
  187. $post = Yii::$app->request->post();
  188. $iv = $post['iv'];
  189. $encryptedData = $post['encryptedData'];
  190. $sjId = $this->sjId;
  191. $merchant = $this->sj->attributes;
  192. $appId = $merchant['miniAppId'];
  193. $miniOpenId = $this->user['miniOpenId'];
  194. $user = $this->user;
  195. if (!empty($user) && !empty($user['mobile'])) {
  196. $userId = $user['id'];
  197. util::success(['mobile' => $user['mobile']]);
  198. }
  199. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  200. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  201. if (empty($sessionKey)) {
  202. util::fail('系统错误,sesstion empty');
  203. }
  204. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  205. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  206. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  207. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  208. if ($errCode != 0) {
  209. util::fail('解密失败');
  210. }
  211. $arr = Json::decode($result);
  212. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  213. if (empty($mobile)) {
  214. util::fail('没有获取手机号,请重试');
  215. }
  216. $userId = $user['id'];
  217. UserService::becomeMember($user, $merchant, ['mobile' => $mobile]);
  218. util::success(['mobile' => $mobile]);
  219. }
  220. //密码修改
  221. public function actionPassword()
  222. {
  223. $post = Yii::$app->request->post();
  224. $old = isset($post['old']) ? $post['old'] : '';
  225. $new = isset($post['new']) ? $post['new'] : '';
  226. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  227. if (empty($new)) {
  228. util::fail('请输入新密码');
  229. }
  230. if ($new != $confirm) {
  231. util::fail('二次密码不一致');
  232. }
  233. $userId = $this->adminId;
  234. $user = UserService::getUserInfo($userId);
  235. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  236. if (empty($old)) {
  237. util::fail('请填写旧密码');
  238. }
  239. if (password_verify($old, $user['payPassword']) == false) {
  240. util::fail('旧密码错误');
  241. }
  242. }
  243. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  244. util::complete('修改成功');
  245. }
  246. //申请会员 ssh 2019.12.18
  247. public function actionApplyMember()
  248. {
  249. $post = Yii::$app->request->post();
  250. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  251. if (stringUtil::isMobile($mobile) == false) {
  252. util::fail('请输入手机号');
  253. }
  254. $userId = $this->adminId;
  255. $asset = UserAssetService::getByUserId($userId);
  256. if (isset($asset['member']) && $asset['member'] != -1) {
  257. util::fail('已经是会员了');
  258. }
  259. $code = isset($post['code']) ? $post['code'] : '';
  260. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  261. if (empty($cacheCode) || $cacheCode != $code) {
  262. util::fail('验证码错误');
  263. }
  264. $update = [];
  265. if (isset($post['realName']) && !empty($post['realName'])) {
  266. $update['realName'] = $post['realName'];
  267. }
  268. if (isset($post['birthday']) && !empty($post['birthday'])) {
  269. $update['birthday'] = $post['birthday'];
  270. }
  271. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  272. if ($userId == $user['id']) {
  273. util::fail('手机号已验证');
  274. }
  275. if (!empty($user)) {
  276. util::fail('手机号已经注册过了');
  277. }
  278. $update['mobile'] = $mobile;
  279. UserService::becomeMember($this->user, $this->sj->attributes, $update);
  280. util::complete('注册成功');
  281. }
  282. //用户基本信息和资产 ssh 2019.12.19
  283. public function actionMyProfile()
  284. {
  285. $custom = $this->custom;
  286. $growth = 100;
  287. $id = $custom['id'] ?? 0;
  288. $name = $custom['name'] ?? '';
  289. $user = [
  290. 'id' => $id,
  291. 'userName' => $name,
  292. 'member' => 2,
  293. ];
  294. $nextLevelGrowth = MerchantExtendClass::getNextLevelGrowth($growth, $this->sjExtend);
  295. $user['userAsset'] = [
  296. 'nextLevelGrowth' => $nextLevelGrowth,
  297. 'growth' => 0,
  298. 'balance' => 0,
  299. 'member' => 2,
  300. 'discount' => 0.88
  301. ];
  302. util::success($user);
  303. }
  304. //登陆客户的信息 ssh 2020.3.12
  305. public function actionLoginDetail()
  306. {
  307. $user = $this->user;
  308. util::success($user);
  309. }
  310. }