Companys.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. class Companys extends Api
  6. {
  7. protected $noNeedLogin = ['getList','getInfo'];
  8. protected $noNeedRight = '*';
  9. protected $model = null;
  10. public function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->model = Db::name('company');
  14. }
  15. /**
  16. * 列表
  17. * @return void
  18. */
  19. public function getList()
  20. {
  21. try {
  22. $page = $this->request->param('page',1);
  23. $limit = $this->request->param('listrow',15);
  24. $offset = ($page - 1) * $limit;
  25. //获取门店信息
  26. $params = [
  27. 'lng' => $this->request->param('lng',''),
  28. 'lat' => $this->request->param('lat',''),
  29. ];
  30. $companyList = $this->getCompanySort($params);
  31. $result = array_slice($companyList['data'],$offset,$limit);
  32. $this->success('获取成功',$result);
  33. } catch (Exception $e) {
  34. $this->error($e->getMessage());
  35. }
  36. }
  37. /**
  38. * 获取门店距离排序
  39. * @return void
  40. */
  41. public function getCompanySort($params=[])
  42. {
  43. try {
  44. $result = [
  45. 'status' => true,
  46. 'msg' => '获取成功',
  47. 'data' => [],
  48. ];
  49. $lng = isset($params['lng']) ? $params['lng'] : '';
  50. $lat = isset($params['lat']) ? $params['lat'] : '';
  51. $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
  52. $where['status'] = 1;
  53. $data = $this->model->where($where)->field($field)->select();
  54. if (!empty($data)) {
  55. foreach ($data as $key => &$value) {
  56. $value['image'] = !empty($value['image']) ? cdnurl($value['image']) : '';
  57. $value['distance'] = getDistance($lng, $lat, $value['longitude'], $value['latitude'], true, 2);
  58. $value['distance_text'] = !empty($value['distance']) ? $value['distance'].'km' : '';
  59. }
  60. $n = count($data);
  61. for($i=1;$i<$n;$i++) { //从第二个元素开始插入
  62. for($j=$i-1;$j>=0;$j--) { //与前面的数比较,找到插入的位置
  63. if($data[$j]['distance'] > $data[$j+1]['distance']) { //比前面的数小,交换位置
  64. $tmp = $data[$j];
  65. $data[$j] = $data[$j+1];
  66. $data[$j+1] = $tmp;
  67. } else { //大于或等于前面的数,表示已找到插入的位置
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. $result['data'] = $data;
  74. return $result;
  75. } catch (Exception $e) {
  76. $result['status'] = 0;
  77. $result['msg'] = $e->getMessage();
  78. return $result;
  79. }
  80. }
  81. /**
  82. * 详情
  83. * @return void
  84. */
  85. public function getInfo()
  86. {
  87. try {
  88. $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
  89. $where['status'] = 1;
  90. $result = $this->model->where($where)->field($field)->find();
  91. if (!empty($result)) {
  92. !empty($result['image']) && $result['image'] = cdnurl($result['image']);
  93. }
  94. $this->success('获取成功',$result);
  95. } catch (Exception $e) {
  96. $this->error($e->getMessage());
  97. }
  98. }
  99. /**
  100. * 绑定门店
  101. * @return void
  102. */
  103. public function bind()
  104. {
  105. Db::startTrans();
  106. try {
  107. $id = $this->request->param('id',0);
  108. $comeform = $this->request->param('comeform','');//'自然进店','平台引流','线下新客','老带新'
  109. $where['id'] = $id;
  110. $where['status'] = 1;
  111. $company = $this->model->where($where)->find();
  112. if (empty($company)) {
  113. throw new Exception('未找到门店信息');
  114. }
  115. $user = $this->auth->getUser();
  116. $userId = $this->auth->id;
  117. if ($user['company_id'] != $id) {//更新绑定门店
  118. $userWhere['id'] = $userId;
  119. $userData['company_id'] = $id;
  120. $userRes = model('User')->where($userWhere)->update($userData);
  121. if (!$userRes) {
  122. throw new Exception('绑定门店失败');
  123. }
  124. }
  125. //用户门店钱包
  126. $walletWhere['user_id'] = $userId;
  127. $walletWhere['company_id'] = $id;
  128. $userWallet = model('UserWallet')->where($walletWhere)->find();
  129. if (empty($userWallet)) {
  130. $walletData = [
  131. 'user_id' => $userId,
  132. 'company_id' => $id,
  133. 'money' => 0.00,
  134. 'comefrom' => $comeform,
  135. ];
  136. $walletRes = model('UserWallet')->insertGetId($walletData);
  137. if (!$walletRes) {
  138. throw new Exception('绑定失败');
  139. }
  140. }
  141. Db::commit();
  142. $this->success();
  143. } catch (Exception $e) {
  144. Db::rollback();
  145. $this->error($e->getMessage());
  146. }
  147. }
  148. /**
  149. * 保存
  150. * @return void
  151. */
  152. public function save()
  153. {
  154. try {
  155. //验证参数
  156. $id = $this->request->param('id',0);
  157. $userId = $this->auth->id;
  158. $scene = !empty($id) ? 'edit' : 'add';
  159. $validate = validate('Companys');
  160. if(!$validate->check($this->request->param(),[],$scene)){
  161. throw new Exception($validate->getError());
  162. }
  163. if (!empty($id)) {
  164. $where['user_id'] = $userId;
  165. $where['id'] = $id;
  166. $companyData = $this->model->where($where)->find();
  167. if (empty($companyData)) {
  168. throw new Exception('未找到相关信息');
  169. }
  170. } else {
  171. $where['user_id'] = $userId;
  172. $companyData = $this->model->where($where)->find();
  173. if (!empty($companyData)) {
  174. throw new Exception('您已申请过入驻');
  175. }
  176. }
  177. $time = time();
  178. $areaData = getProvince($this->request->param());
  179. $fullAddress = $areaData['full_address'];
  180. $data = [
  181. 'contacts' => $this->request->param('contacts', ''),
  182. 'mobile' => $this->request->param('mobile', ''),
  183. 'province_id' => $this->request->param('province_id', 0),
  184. 'city_id' => $this->request->param('city_id', 0),
  185. 'area_id' => $this->request->param('area_id', 0),
  186. 'province_name' => $areaData['province_name'],
  187. 'city_name' => $areaData['city_name'],
  188. 'area_name' => $areaData['area_name'],
  189. 'address' => $this->request->param('address', ''),
  190. 'full_address' => $fullAddress,
  191. 'aptitude_images' => $this->request->param('aptitude_images', ''),
  192. 'status' => 1,
  193. ];
  194. if (empty($id)) {
  195. $data['user_id'] = $userId;
  196. $data['createtime'] = $time;
  197. $res = $this->model->insertGetId($data);
  198. } else {
  199. $data['updatetime'] = $time;
  200. $where['id'] = $id;
  201. $where['user_id'] = $userId;
  202. $res = $this->model->where($where)->update($data);
  203. }
  204. if (!$res) {
  205. throw new Exception('操作失败');
  206. }
  207. $this->success('操作成功');
  208. } catch (Exception $e) {
  209. $this->error($e->getMessage());
  210. }
  211. }
  212. }