IndexController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020-09-12
  6. * Time: 9:33
  7. */
  8. namespace api\js\controller;
  9. use api\js\model\JsModel;
  10. use cmf\controller\RestBaseController;
  11. use think\Db;
  12. use think\Log;
  13. class IndexController extends RestBaseController
  14. {
  15. public function index()
  16. {
  17. $navList = Db::name('index_nav')->where('pos',1)->order('list_order','asc')->select()->each(function ($navList){
  18. $navList['icon'] = cmf_get_image_preview_url($navList['icon']);
  19. return $navList;
  20. });
  21. $banner = Db::name('slide_item')->where('slide_id',1)->where('status',1)->order('list_order','asc')->select()->each(function ($banner){
  22. $banner['image'] = cmf_get_image_preview_url($banner['image']);
  23. return $banner;
  24. });
  25. $lat = $this->request->param('lat',1);//维度
  26. $lng = $this->request->param('lng',1);//经度
  27. $lat = $lat > 1 ? $lat :37.475375;
  28. $lng = $lng> 1 ? $lng :121.373573;
  29. $JsModel = new JsModel();
  30. $jsList = $JsModel->getLits([],$lat,$lng);
  31. $this->success('ok',['nav'=>$navList,'banner'=>$banner,'js'=>$jsList]);
  32. }
  33. public function getRegion()
  34. {
  35. $parentId = $this->request->param('parentId');
  36. $pdepth = $this->request->param('depth',1);
  37. if($parentId){
  38. $where['parentId'] = $parentId;
  39. }
  40. $where['depth'] = $pdepth;
  41. $list = Db::name('region')->where($where)->field('id,cityName')->select();
  42. $this->success('ok',$list);
  43. }
  44. public function getOut()
  45. {
  46. $list = Db::name('project_out')->select();
  47. $this->success('ok',$list);
  48. }
  49. public function getWXLog()
  50. {
  51. $this->success('ok',1);
  52. }
  53. public function getdow()
  54. {
  55. $info = cmf_get_option('dow');
  56. $this->success('ok',$info);
  57. }
  58. public function getKefu()
  59. {
  60. $info = Db::name('portal_post')->where('id',6)->find();
  61. $this->success('ok',$info['phone']);
  62. }
  63. public function bindWechat(){
  64. $validate = new \think\Validate([
  65. 'mobile' => 'require',
  66. 'code' => 'require',
  67. 'openid' => 'require',
  68. ]);
  69. $validate->message([
  70. 'mobile.require' => '请输入您的手机号',
  71. 'code.require' => '验证码不能为空!',
  72. 'openid' => '微信授权失败!'
  73. ]);
  74. $data = $this->request->param();
  75. if (!$validate->check($data)) {
  76. $this->error($validate->getError());
  77. }
  78. $type = $data['type'];
  79. unset($data['type']);
  80. $findUserWhere = [];
  81. if (cmf_check_mobile($data['mobile'])) {
  82. $findUserWhere['mobile'] = $data['mobile'];
  83. } else {
  84. $this->error('手机号格式错误');
  85. }
  86. if ($type == 1) {
  87. $findUserWhere['user_type'] = 2;
  88. }
  89. if ($type == 2) {
  90. $findUserWhere['user_type'] = 3;
  91. }
  92. $isExist = UserModel::name("user")->where('user_type', $findUserWhere['user_type'])
  93. ->where('openid', $data['openid'])->count();
  94. if (!$isExist) {
  95. $findUser = UserModel::name("user")->where($findUserWhere)->find();
  96. if (empty($findUser)) {
  97. // 用户不存在
  98. } else {
  99. if ($findUser->openid) {
  100. $this->error("账号已绑定其他微信!");
  101. }
  102. Db::name('user')->where('id', $findUser['id'])->update(['openid' => $data['openid']]);
  103. }
  104. if (empty($this->deviceType) && (empty($data['device_type']) || !in_array($data['device_type'], $this->allowedDeviceTypes))) {
  105. $this->error("请求错误,未知设备!");
  106. } else if (!empty($data['device_type'])) {
  107. $this->deviceType = $data['device_type'];
  108. }
  109. $findUserToken = Db::name("user_token")
  110. ->where('user_id', $findUser['id'])
  111. ->where('device_type', $this->deviceType)
  112. ->find();
  113. $currentTime = time();
  114. $expireTime = $currentTime + 24 * 3600 * 180;
  115. $token = md5(uniqid()) . md5(uniqid());
  116. if (empty($findUserToken)) {
  117. $result = Db::name("user_token")->insert([
  118. 'token' => $token,
  119. 'user_id' => $findUser['id'],
  120. 'expire_time' => $expireTime,
  121. 'create_time' => $currentTime,
  122. 'device_type' => $this->deviceType
  123. ]);
  124. } else {
  125. $result = Db::name("user_token")
  126. ->where('user_id', $findUser['id'])
  127. ->where('device_type', $this->deviceType)
  128. ->update([
  129. 'token' => $token,
  130. 'expire_time' => $expireTime,
  131. 'create_time' => $currentTime
  132. ]);
  133. }
  134. if (empty($result)) {
  135. $this->error("登录失败!");
  136. }
  137. Db::name('user')->where('id', $findUser['id'])->update(['last_login_ip' => get_client_ip(), 'last_login_time' => time()]);
  138. $this->success("登录成功!", ['token' => $token, 'id' => $findUser['id']]);
  139. } else {
  140. $this->error("微信已绑定其他账号!");
  141. }
  142. }
  143. }