AlbumController.php 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\AgentBannerModel;
  4. use App\Models\AlbumAgentModel;
  5. use App\Models\AlbumBannerModel;
  6. use App\Models\AlbumCatModel;
  7. use App\Models\AlbumCommentsModel;
  8. use App\Models\AlbumFavoriteModel;
  9. use App\Models\AlbumFormId;
  10. use App\Models\AlbumInformationModel;
  11. use App\Models\AlbumManufacturerModel;
  12. use App\Models\AlbumNavModel;
  13. use App\Models\AlbumNewsModel;
  14. use App\Models\AlbumProductModel;
  15. use App\Models\AlbumProductPriceModel;
  16. use App\Models\AlbumProductStyleModel;
  17. use App\Models\AlbumUserModel;
  18. use App\Models\AlbumWatchRecord;
  19. use App\Models\AlbumXyxUserModel;
  20. use App\Models\CustomerCatRecordModel;
  21. use App\Models\CustomerDetailsModel;
  22. use App\User;
  23. use Illuminate\Http\Request;
  24. use Illuminate\Support\Facades\DB;
  25. use Validator, Response,Auth;
  26. use App\Services\Base\ErrorCode;
  27. use WXBizDataCrypt;
  28. use EasyWeChat\Factory;
  29. class AlbumController extends Controller
  30. {
  31. private $wechat_app;
  32. public function albumSetting(Request $request)
  33. {
  34. $validator = Validator::make($request->all(), [
  35. 'store_id' => 'required',
  36. ], [
  37. 'store_id.required' => '店铺信息未知',
  38. ]);
  39. if ($validator->fails()) {
  40. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
  41. }
  42. $store_id = $request->store_id;
  43. //小程序設置
  44. $setting = AlbumManufacturerModel::where('store_id',$store_id)->first();
  45. //轮播图
  46. $banner = AlbumBannerModel::where('store_id',$store_id)->orderByDesc('sort')->get();
  47. //底部导航栏
  48. $navbar = AlbumNavModel::where([['store_id',$store_id],['state',0]])->orderByDesc('sort')->get();
  49. //顶部菜单栏
  50. $menus = AlbumNavModel::where([['store_id',$store_id],['state',1]])->orderByDesc('sort')->get();
  51. $news = AlbumNewsModel::where([['store_id',$store_id]])->orderByDesc('sort')->paginate(5);
  52. return $this->api(compact('setting','banner','news','navbar','menus'));
  53. }
  54. /**
  55. * @api {post} /api/album/login 登陆(login)
  56. * @apiDescription 登陆(login)
  57. * @apiGroup Album
  58. * @apiPermission none
  59. * @apiVersion 0.1.0
  60. * @apiParam {string} code 小程序登录生成的code
  61. * @apiParam {string} nickName 微信昵称
  62. * @apiParam {string} avatar 微信头像
  63. * @apiSuccessExample {json} Success-Response:
  64. * HTTP/1.1 200 OK
  65. * {
  66. * "status": true,
  67. * "status_code": 0,
  68. * "message": "",
  69. * "data": {
  70. *
  71. * }
  72. * }
  73. * @apiErrorExample {json} Error-Response:
  74. * HTTP/1.1 400 Bad Request
  75. * {
  76. * "state": false,
  77. * "code": 1000,
  78. * "message": "传入参数不正确",
  79. * "data": null or []
  80. * }
  81. * 可能出现的错误代码:
  82. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  83. */
  84. public function albumXcxLogin(Request $request)
  85. {
  86. $validator = Validator::make($request->all(),
  87. [
  88. 'code' => 'required',
  89. 'store_id' => 'required'
  90. /* 'nickName' => 'required',
  91. 'avatar' => 'required',*/
  92. ],
  93. [
  94. 'code.required' => 'code不能为空!',
  95. 'store_id.required' => 'store_id不能为空!'
  96. /*'nickName.required' => 'nickName不能为空!',
  97. 'avatar.required' => 'avatar不能为空!',*/
  98. ]
  99. );
  100. if ($validator->fails()) {
  101. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  102. }
  103. $storeid = $request->get('store_id');
  104. $this->wechat_app = AlbumManufacturerModel::where('store_id',$storeid)->first();
  105. $config = [
  106. 'app_id' => $this->wechat_app->app_id,
  107. 'secret' => $this->wechat_app->app_secret,
  108. // 下面为可选项
  109. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  110. 'response_type' => 'array',
  111. ];
  112. $app = Factory::miniProgram($config);
  113. $code = $request->get('code');
  114. $session = $app->auth->session($code);
  115. \Log::info(json_encode($session));
  116. $openid = $session['openid'];
  117. $userinfo = AlbumUserModel::where('wechat_open_id', $openid)->where('store_id',$storeid)->first(['id', 'username','wechat_open_id','avatar']);
  118. if (!$userinfo) {
  119. $data['wechat_open_id'] = $openid;
  120. $data['username'] = $request->get('nickName');
  121. $data['avatar'] = $request->get('avatar');
  122. $data['is_dealer'] = 0;
  123. $userinfo = AlbumUserModel::create($data);
  124. }
  125. if (Auth::loginUsingId($userinfo->id)) {
  126. $user = Auth::user();
  127. $token = $user->createToken($user->id . '-' . $user->openid)->accessToken;
  128. return $this->api(compact('token', 'user', 'session_key'));
  129. } else {
  130. return $this->error(ErrorCode::INCORRECT_USER_OR_PASS);
  131. }
  132. }
  133. /**
  134. * @api {post} /api/album/xyx_login 登陆(xyx_login)
  135. * @apiDescription 登陆(xyx_login)
  136. * @apiGroup Album
  137. * @apiPermission none
  138. * @apiVersion 0.1.0
  139. * @apiParam {int} [code]
  140. * @apiParam {int} [agent_id] 经销商id
  141. * @apiParam {int} [store_id] 商户id
  142. * @apiSuccessExample {json} Success-Response:
  143. * HTTP/1.1 200 OK
  144. * {
  145. * "status": true,
  146. * "status_code": 0,
  147. * "message": "",
  148. * "data": {
  149. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImIyNGI3MzY3MDNmZDg3MGFjNTg2MWIxNDUzMDcyYjljYzFmNmJhMzE2NTAxZWVlNGYzM2M5MTIzNzFmNGYzZjg2MTY1M2YxMjE2YzE4OTFiIn0.eyJhdWQiOiI5IiwianRpIjoiYjI0YjczNjcwM2ZkODcwYWM1ODYxYjE0NTMwNzJiOWNjMWY2YmEzMTY1MDFlZWU0ZjMzYzkxMjM3MWY0ZjNmODYxNjUzZjEyMTZjMTg5MWIiLCJpYXQiOjE1NDA3OTYyMTYsIm5iZiI6MTU0MDc5NjIxNiwiZXhwIjoxNTcyMzMyMjE1LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.Ruihvl_HMCAHx9XDfeckz48Q_72kfG7vWDsqDrZWbrjq-yuNpM3LrUF-nlsKjedo97BwU_Apz-jnGVqr0ONKZlH-aBKDmIgM2aUZfFADsHhHNJH_oU2fMe1bOrjfUp-PJxfVWQS8c8h5RYmeqtzAYCKaD4P7OZbLmOX0YosKghpaGtiyE65s6jtrOXRhAzXAhYsZToQNBMokFVRqGih9EWuVp6EVwRtI3cb4IV6iQf08cX9DGlLLBJJzLWR5ZWgxJLAGrj5iMCWBl4JKVR8Fsy-xnx3EtrB7ODpJH6hB_u3XnUSAFuaG-KzMPLsj45iqg2hy53er2xtcfAowQFsPoAbZZu0n8c9hWt4GLemR6N1iZ3LBViv3tL5BK52pinQzAnmvltAhJP1YhfTOOjzkxcYVh2JeJagufEoxeeUarMMn21gQSFEzKFCimI7zZQxhT9leLpUn51DLD8Mu87NpYb3JN-JFbOlIDA7bDN3bNto7uuSuC4eBwPo3Ge0StuW4kjHsDTcrVF2He_8FdrPcPpGcvQsXQgv4vwlEhbL8dTlrOodum7H_tY_qUxUG1pEg6bfpn_82ej-AOSc9xi0nrhdcS9y_Z68rcwOJu8esI1pXm9TeQdZIDG85y_t74At8XN6oFcIM__6_dTne_3DHwijW-uOLupNky-5J4ILmZck",
  150. * "user": {
  151. * "user_id": 1,
  152. * "is_agent": 1,
  153. * "agent_id": 4
  154. * }
  155. * }
  156. * }
  157. * @apiErrorExample {json} Error-Response:
  158. * HTTP/1.1 400 Bad Request
  159. * {
  160. * "state": false,
  161. * "code": 1000,
  162. * "message": "传入参数不正确",
  163. * "data": null or []
  164. * }
  165. * 可能出现的错误代码:
  166. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  167. */
  168. public function albumXyxLogin(Request $request)
  169. {
  170. $datas = $request->input();
  171. $validator = Validator::make($request->all(),
  172. [
  173. 'code' => 'required',
  174. 'agent_id' => 'required',
  175. 'nickName' => 'required',
  176. 'iv' => 'required',
  177. 'encryptData' => 'required',
  178. 'avatar' => 'required',
  179. 'store_id' => 'required'
  180. ],[
  181. 'code.required' => 'code不能为空!',
  182. 'agent_id.required' => 'agent_id不能为空!',
  183. 'nickName.required' => '昵称不能为空!',
  184. 'iv.required' => '昵称不能为空!',
  185. 'encryptData.required' => '昵称不能为空!',
  186. 'avatar.required' => '头像不能为空!',
  187. 'store_id.required' => '站点ID不能为空!'
  188. ]
  189. );
  190. if ($validator->fails()) {
  191. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  192. }
  193. $this->wechat_app = AlbumManufacturerModel::where('store_id',$datas['store_id'])->first();
  194. if(!$this->wechat_app) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  195. $config = [
  196. 'app_id' => $this->wechat_app->xyx_id,
  197. 'secret' => $this->wechat_app->xyx_secret,
  198. // 下面为可选项
  199. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  200. 'response_type' => 'array',
  201. ];
  202. $app = Factory::miniProgram($config);
  203. $res = $app->auth->session($datas['code']);
  204. // dd($res);
  205. if (!$res || empty($res['openid'])) {
  206. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '获取用户OpenId失败!', $validator->messages());
  207. }
  208. $session = $res['session_key'];
  209. $decryptedData = $app->encryptor->decryptData($session, $datas['iv'], $datas['encryptData']);
  210. dd($decryptedData);die;
  211. $check = AlbumUserModel::where('wechat_open_id',$res['openid'])->first();
  212. if(!$check){
  213. $add['username'] = preg_replace('/[\xf0-\xf7].{3}/', '', $datas['nickName']);
  214. $add['wechat_open_id'] = $res['openid'];
  215. $add['wechat_union_id'] = '';
  216. $add['avatar'] = $datas['avatar'];
  217. $add['is_agent'] = 0;
  218. $add['model'] = 1;
  219. $add['store_id'] = $datas['store_id'];
  220. $res = AlbumUserModel::create($add);
  221. // dd($res);
  222. if($res){
  223. $check = AlbumUserModel::where([['wechat_open_id',$res->wechat_open_id],['store_id',$datas['store_id']]])->first();
  224. $user = [
  225. 'user_id'=>$check['id'],
  226. //'avatar'=>$check['avatar'],
  227. // 'username'=>$check['username'],
  228. 'phone'=>$check->phone,
  229. 'is_agent'=>0,
  230. 'agent_id'=>'',
  231. ];
  232. }else{
  233. $user=[
  234. 'error'=>1
  235. ];
  236. }
  237. } else {
  238. //print_r($check);die;
  239. $save['username'] = preg_replace('/[\xf0-\xf7].{3}/', '', $datas['nickName']);
  240. $save['avatar'] = $datas['avatar'];
  241. $save['model'] = 1;
  242. $res = AlbumUserModel::where('wechat_open_id',$res['openid'])->update($save);
  243. //echo 111;
  244. //print_r($check);die;
  245. if($check->is_dealer == 1){
  246. $user_agent = AlbumAgentModel::where([['user_id',$check->id],['status',1]])->first();
  247. $agent_id = $user_agent['id'];
  248. } else {
  249. $agent_id = '';
  250. }
  251. $user = [
  252. 'user_id'=>$check->id,
  253. 'phone'=>$check->phone,
  254. //'avatar'=>$datas['avatar'],
  255. //'username'=>$datas['nickName'],
  256. 'is_agent'=>$check->is_dealer,
  257. 'agent_id'=>$agent_id,
  258. ];
  259. }
  260. // echo 111;die;
  261. if (Auth::loginUsingId($check->id)) {
  262. $userAuth = Auth::user();
  263. if($check->is_dealer ==0){
  264. if($request->input('agent_id')!=0){
  265. $userAuth->up_agent_id = $request->input('agent_id');
  266. }
  267. $add_record['agent_id'] = $request->input('agent_id');
  268. $add_record['open_id'] = $check->wechat_open_id;
  269. $add_record['action'] = 4;
  270. $add_record['store_id'] = $datas['store_id'];
  271. $add_record['detail'] = null;
  272. AlbumWatchRecord::create($add_record);
  273. // dd($add_record);
  274. // print_r($add_record['agent_id']);die;
  275. $user_agent = AlbumAgentModel::where('id',$add_record['agent_id'])->first();
  276. //dd($user_agent);
  277. if($user_agent){
  278. $agent = AlbumUserModel::where('id',$user_agent->user_id)->first();
  279. try{
  280. $this->sendLogsMessage($datas['store_id'],$agent->wechat_open_id,4,$userAuth->username);
  281. }catch (\Exception $e){
  282. }
  283. }
  284. }else{
  285. $userAuth->up_agent_id = 0;
  286. }//dd($userAuth);
  287. $userAuth->save();
  288. $user['up_agent_id'] = $userAuth->up_agent_id;
  289. $token = $userAuth->createToken($userAuth->id . '-' . $userAuth->openid)->accessToken;
  290. return $this->api(compact('token', 'user'));
  291. } else {
  292. return $this->error(ErrorCode::INCORRECT_USER_OR_PASS);
  293. }
  294. }
  295. /*
  296. * curl get请求
  297. */
  298. public function curl_get($url)
  299. {
  300. //初始化
  301. $ch = curl_init();
  302. //设置选项,包括URL
  303. curl_setopt($ch, CURLOPT_URL, $url);
  304. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  305. curl_setopt($ch, CURLOPT_HEADER, 0);
  306. $output = curl_exec($ch);
  307. curl_close($ch);
  308. return $output;
  309. }
  310. private function getOpenid($code)
  311. {
  312. $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_app->app_id}&secret={$this->wechat_app->app_secret}&js_code={$code}&grant_type=authorization_code";
  313. $res = $this->curl_get($api);
  314. $res = json_decode($res, true);
  315. return $res;
  316. }
  317. private function getOpenidxyx($code)
  318. {
  319. //$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_app->xyx_id}&secret={$this->wechat_app->xyx_secret}&js_code={$code}&grant_type=authorization_code";
  320. $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_app->xyx_id}&secret={$this->wechat_app->xyx_secret}&js_code={$code}&grant_type=authorization_code";
  321. $res = $this->curl_get($api);
  322. $res = json_decode($res, true);
  323. return $res;
  324. }
  325. /**
  326. * @api {get} /api/album/goods 商品列表(goods)
  327. * @apiDescription 商品列表(goods)
  328. * @apiGroup Album
  329. * @apiPermission none
  330. * @apiVersion 0.1.0
  331. * @apiParam {int} [cat_id] 分类id
  332. * @apiParam {string} [keywords] 关键词.
  333. * @apiParam {int} [status] 状态 1 只选择热销 2 只选择最新上市 3只选择一个风格 4 同时选择热销和最新上市 5同时选择热销和风格 6同时选择最新上市和风格 7全选 0全不选
  334. * @apiParam {int} [style] 风格
  335. * @apiSuccessExample {json} Success-Response:
  336. * HTTP/1.1 200 OK
  337. * {
  338. * "status": true,
  339. * "status_code": 0,
  340. * "message": "",
  341. * "data": {
  342. * "goods": [
  343. * {
  344. * "id": 3,
  345. * "cover_pic": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  346. * "name": "简约",
  347. * "style": "纯白",
  348. * "hot_cake": 1,
  349. * "news": 1,
  350. * "price": ""
  351. * }
  352. * ],
  353. * "name": 品牌名
  354. * }
  355. * }
  356. * @apiErrorExample {json} Error-Response:
  357. * HTTP/1.1 400 Bad Request
  358. * {
  359. * "state": false,
  360. * "code": 1000,
  361. * "message": "传入参数不正确",
  362. * "data": null or []
  363. * }
  364. * 可能出现的错误代码:
  365. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  366. */
  367. public function albumGoods(Request $request)
  368. {
  369. $userAuth = Auth('api')->user();
  370. if(!$userAuth) return $this->error(ErrorCode::ERROR_POWER, '登陆过期!');
  371. $validator = Validator::make($request->all(), [
  372. 'cat_id' => 'required',
  373. 'store_id' => 'required',
  374. 'status' => 'required',
  375. ],[
  376. 'cat_id.required'=>'缺少分类参数',
  377. 'store_id.required'=>'缺少商户参数',
  378. 'status.required'=>'缺少状态参数',
  379. ]);
  380. if ($validator->fails()) {
  381. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  382. }
  383. $data = $request->input();
  384. //dd($data);
  385. $cat_id = request('cat_id');
  386. $store_id = request('store_id');
  387. $status = request('status');
  388. $style = request('style');
  389. $select_info = ['cat_id'=>$cat_id,'style'=>$style];
  390. if($userAuth->up_agent_id!=0&&$cat_id!="undefined"&&$style!="undefined"){
  391. $add_record['agent_id'] = $userAuth->up_agent_id;
  392. $add_record['open_id'] = $userAuth->wechat_open_id;
  393. $add_record['action'] = 2;
  394. $add_record['store_id'] = $store_id;
  395. $add_record['detail'] = json_encode($select_info);
  396. AlbumWatchRecord::create($add_record);
  397. $cat_record['agent_id'] = $userAuth->up_agent_id;
  398. $cat_record['open_id'] = $userAuth->wechat_open_id;
  399. $cat_record['store_id'] = $store_id;
  400. $cat_record['style_id'] = $style;
  401. $cat_record['cat_id'] = $cat_id;
  402. CustomerCatRecordModel::create($cat_record);
  403. $user_agent = AlbumAgentModel::where('id',$userAuth->up_agent_id)->first();
  404. $agent = AlbumUserModel::where('id',$user_agent->user_id)->first();
  405. try{
  406. $this->sendLogsMessage($store_id,$agent->wechat_open_id,2,$userAuth->username);
  407. }catch (\Exception $e){
  408. }
  409. }
  410. if(isset($data['keywords'])&&!empty($data['keywords'])) {
  411. $keywords = '%'.$data['keywords'].'%';
  412. $goods = AlbumProductModel::where([['name','like',$keywords],['store_id',$data['store_id']],['cat_id',$cat_id],['store_id',$store_id]]);
  413. } else {
  414. $goods = AlbumProductModel::where([['cat_id',$cat_id],['store_id',$store_id]]);
  415. }
  416. if($status == 1) {
  417. $goods->where([['hot_cake', 1]]);
  418. }
  419. if($status == 2){
  420. $goods->where('news',1);
  421. }
  422. if($status == 3){
  423. $goods->where('style',$style);
  424. }
  425. $goods = $goods->select('id','cover_pic','name','style','hot_cake','news','price_default')->orderByDesc('sort')->get();
  426. foreach($goods as $key =>$val){
  427. if($userAuth->is_dealer != 1){
  428. if($userAuth->up_agent_id==0){
  429. $goods[$key]['price'] = '';
  430. }else{
  431. $check = AlbumProductPriceModel::where([['agent_id',$userAuth->up_agent_id],['store_id',$data['store_id']],['product_id',$val['id']]])->first();
  432. if($check){
  433. $goods[$key]['price'] = $check->price;
  434. }else{
  435. $goods[$key]['price'] = '';
  436. }
  437. }
  438. }elseif($userAuth->is_dealer == 1){
  439. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  440. $agent_id = $user_agent['id'];
  441. $check = AlbumProductPriceModel::where([['agent_id',$agent_id],['store_id',$data['store_id']],['product_id',$val['id']]])->first();
  442. if($check){
  443. $goods[$key]['price'] = $check->price;
  444. }else{
  445. $goods[$key]['price'] = '';
  446. }
  447. }
  448. }
  449. $data =AlbumManufacturerModel::where('store_id',$store_id)->first();
  450. $name = $data['name'];
  451. return $this->api(compact('goods','name'));
  452. }
  453. /**
  454. * @api {get} /api/album/goods-detail 商品详细(goods-detail)
  455. * @apiDescription 商品列表(goods-detail)
  456. * @apiGroup Album
  457. * @apiPermission none
  458. * @apiVersion 0.1.0
  459. * @apiParam {int} [goods_id] 商品id
  460. * @apiParam {int} [store_id] 商户id 模拟值为0
  461. * @apiSuccessExample {json} Success-Response:
  462. * HTTP/1.1 200 OK
  463. * {
  464. * "status": true,
  465. * "status_code": 0,
  466. * "message": "",
  467. * "data": {
  468. * "id": 3,
  469. * "store_id": 0,
  470. * "cat_id": 9,
  471. * "specifications_img": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  472. * "style": "纯白",
  473. * "sort": 11,
  474. * "cover_pic": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  475. * "hot_cake": 1,
  476. * "news": 1,
  477. * "detail": "<p>达达娃达娃达娃大青蛙大全大是大非如果v率v恶策但是许多女性的项目部的不行么法可没副本副本副本么方便</p>",
  478. * "created_at": "2018-05-19 15:09:04",
  479. * "updated_at": "2018-05-19 15:09:04",
  480. * "deleted_at": null,
  481. * "install_img": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  482. * "name": "简约",
  483. * "detail_pic": ""
  484. * "price": "",
  485. * "price_default": "",
  486. * "mobile": "",
  487. * "address": "",
  488. * "realname": "",
  489. * "is_favorite": 1,
  490. * "favorite_id": 4
  491. * }
  492. * }
  493. * @apiErrorExample {json} Error-Response:
  494. * HTTP/1.1 400 Bad Request
  495. * {
  496. * "state": false,
  497. * "code": 1000,
  498. * "message": "传入参数不正确",
  499. * "data": null or []
  500. * }
  501. * 可能出现的错误代码:
  502. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  503. */
  504. public function albumGoodsDetail(Request $request)
  505. {
  506. // $userAuth = AlbumXyxUserModel::find(1);
  507. $userAuth = Auth('api')->user();
  508. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  509. $validator = Validator::make($request->all(), [
  510. 'goods_id' => 'required',
  511. 'store_id' => 'required',
  512. ],[
  513. 'goods_id.required'=>'缺少商品参数',
  514. 'store_id_id.required'=>'缺少STORE参数',
  515. ]);
  516. if ($validator->fails()) {
  517. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  518. }
  519. $goods_id = request('goods_id');
  520. $store_id = request('store_id');
  521. $select_info = ['goods_id'=>$goods_id];
  522. if($userAuth->up_agent_id!=0){
  523. $add_record['agent_id'] = $userAuth->up_agent_id;
  524. $add_record['open_id'] = $userAuth->wechat_open_id;
  525. $add_record['action'] = 3;
  526. $add_record['store_id'] = $store_id;
  527. $add_record['detail'] = json_encode($select_info);
  528. AlbumWatchRecord::create($add_record);
  529. $user_agent = AlbumAgentModel::where('id',$userAuth->up_agent_id)->first();
  530. $agent = AlbumUserModel::where('id',$user_agent->user_id)->first();
  531. try{
  532. $this->sendLogsMessage($store_id,$agent->wechat_open_id,3,$userAuth->username);
  533. }catch (\Exception $e){
  534. }
  535. }
  536. $ma = AlbumManufacturerModel::where('store_id',$store_id)->first();
  537. $goods = AlbumProductModel::where([['id',$goods_id],['store_id',$store_id]])->first();
  538. $address = AlbumAgentModel::where('id',$userAuth->up_agent_id)->first();
  539. if(!empty($goods) && $userAuth->is_dealer != 1){
  540. $price = DB::table('album_product_price')->where([['agent_id',$userAuth->up_agent_id],['store_id',$store_id],['product_id',$goods['id']]])->first();
  541. if(!empty($price)){
  542. $goods->price = $price->price;
  543. }else{
  544. $goods->price = '';
  545. }
  546. $check_favorite = AlbumFavoriteModel::where([['user_id',$userAuth->id],['store_id',$store_id],['product_id',$goods['id']]])->first();
  547. //dd($check_favorite);
  548. if($check_favorite){
  549. $goods->is_favorite = 1;
  550. $goods->favorite_id = $check_favorite->id;
  551. }else{
  552. $goods->is_favorite = 0;
  553. $goods->favorite_id = '';
  554. }
  555. }elseif(!empty($goods) && $userAuth->is_dealer == 1){
  556. $agent_id = $address['id'];
  557. $price = DB::table('album_product_price')->where([['agent_id',$agent_id],['store_id',$store_id],['product_id',$goods['id']]])->first();
  558. if(!empty($price)){
  559. $goods->price = $price->price;
  560. }else{
  561. $goods->price = '';
  562. }
  563. $goods->is_favorite = 0;
  564. $goods->favorite_id = '';
  565. }
  566. if($userAuth->up_agent_id == 0 || !$address){
  567. $goods->price = '';
  568. $goods->mobile = $ma->phone;
  569. $goods->address = $ma->address;
  570. $goods->realname = $ma->name;
  571. $goods->avatar = $ma->avatar;
  572. }else{
  573. $agent = AlbumUserModel::where('id',$address->user_id)->first();
  574. $goods->mobile = $address->phone;
  575. $goods->address = $address->address;
  576. $goods->realname = $address->name;
  577. $goods->avatar = $agent->avatar;
  578. }
  579. return $this->api($goods);
  580. }
  581. /**
  582. * @api {get} /api/album/favorite_list 收藏列表(favorite_list)
  583. * @apiDescription 收藏列表(favorite_list)
  584. * @apiGroup Album
  585. * @apiPermission none
  586. * @apiVersion 0.1.0
  587. * @apiParam {int} [store_id] 商户id 模拟值为0
  588. * @apiSuccessExample {json} Success-Response:
  589. * HTTP/1.1 200 OK
  590. * {
  591. * "status": true,
  592. * "status_code": 0,
  593. * "message": "",
  594. * "data": [
  595. * {
  596. * "id": 4,
  597. * "user_id": 1,
  598. * "deleted_at": null,
  599. * "created_at": "2018-10-29 15:51:47",
  600. * "updated_at": "2018-10-29 15:53:15",
  601. * "product_id": 3,
  602. * "store_id": 0,
  603. * "goods": {
  604. * "id": 3,
  605. * "store_id": 0,
  606. * "cat_id": 9,
  607. * "specifications_img": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  608. * "style": "纯白",
  609. * "sort": 11,
  610. * "cover_pic": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  611. * "hot_cake": 1,
  612. * "news": 1,
  613. * "detail": "<p>达达娃达娃达娃大青蛙大全大是大非如果v率v恶策但是许多女性的项目部的不行么法可没副本副本副本么方便</p>",
  614. * "created_at": "2018-05-19 15:09:04",
  615. * "updated_at": "2018-05-19 15:09:04",
  616. * "deleted_at": null,
  617. * "install_img": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  618. * "name": "简约",
  619. * "detail_pic": ""
  620. * }
  621. * }
  622. * ]
  623. * }
  624. * @apiErrorExample {json} Error-Response:
  625. * HTTP/1.1 400 Bad Request
  626. * {
  627. * "state": false,
  628. * "code": 1000,
  629. * "message": "传入参数不正确",
  630. * "data": null or []
  631. * }
  632. * 可能出现的错误代码:
  633. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  634. */
  635. public function albumFavoriteList(Request $request)
  636. {
  637. $userAuth = Auth('api')->user();
  638. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  639. $validator = Validator::make($request->all(), [
  640. 'store_id' => 'required',
  641. ],[
  642. 'store_id.required'=>'缺少商户参数',
  643. ]);
  644. if ($validator->fails()) {
  645. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  646. }
  647. $data = $request->input();
  648. $favorite = AlbumFavoriteModel::where([['user_id',$userAuth->id],['store_id',$data['store_id']]])->get();
  649. foreach ($favorite as $key=>$val) {
  650. $favorite[$key]['goods'] = AlbumProductModel::where([['id',$val['product_id']],['store_id',$data['store_id']]])->first();
  651. }
  652. return $this->api($favorite);
  653. }
  654. /**
  655. * @api {post} /api/album/get-watch-recored 查看客户浏览情况(get-watch-recored)
  656. * @apiDescription 查看客户浏览情况(get-watch-recored)
  657. * @apiGroup Album
  658. * @apiPermission none
  659. * @apiVersion 0.1.0
  660. * @apiParam {int} [store_id] 商户id 模拟值为0
  661. * @apiParam {int} [type] 查询方式 1 按用户 2 按时间 3 按类型
  662. * @apiParam {int} [user_id] user_id
  663. * @apiParam {int} [action] 行为类型 1 收藏 2查看类目 3查看商品 4登陆 5点击图片 6 点击导航 7一键拨号
  664. * @apiParam {int} [start_time] 查询开始时间
  665. * @apiParam {int} [end_time] 查询结束时间
  666. * @apiSuccessExample {json} Success-Response:
  667. * HTTP/1.1 200 OK
  668. * {
  669. * "status": true,
  670. * "status_code": 0,
  671. * "message": "",
  672. * "data": [
  673. * "type1":{ 1 收藏 2查看类目 3查看商品 4登陆 5点击图片 6 点击导航 7一键拨号
  674. * "time": 2018-1-1 10:10:12,
  675. * "action": '做了什么什么都做',
  676. * "user": [
  677. * "username":"王小贱",
  678. * "avatar":"awdawdawdawdawdawd",头像
  679. * "phone":"1123123123123"
  680. * ]
  681. * }
  682. * ...
  683. * "count":6;
  684. * ]
  685. * }
  686. * @apiErrorExample {json} Error-Response:
  687. * HTTP/1.1 400 Bad Request
  688. * {
  689. * "state": false,
  690. * "code": 1000,
  691. * "message": "传入参数不正确",
  692. * "data": null or []
  693. * }
  694. * 可能出现的错误代码:
  695. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  696. */
  697. public function albumGetWatchRecord(Request $request)
  698. {
  699. $userAuth = Auth('api')->user();
  700. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  701. $validator = Validator::make($request->all(), [
  702. 'store_id' => 'required',
  703. 'type'=>'required'
  704. ],[
  705. 'store_id.required'=>'缺少商户参数',
  706. 'type.required'=>'缺少类型参数',
  707. ]);
  708. if ($validator->fails()) {
  709. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  710. }
  711. $type = $request->input('type');
  712. $store_id = $request->input('store_id');
  713. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  714. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  715. $query = AlbumWatchRecord::where([['agent_id',$user_agent->id],['store_id',$store_id]]);
  716. switch ($type){
  717. case '1':
  718. if(!$request->input('user_id')) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!');
  719. $user = AlbumUserModel::find(request('user_id'));
  720. $query = $query->where('open_id',$user->wechat_open_id);
  721. break;
  722. case '2':
  723. if(!$request->input('start_time')||!$request->input('end_time')) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!');
  724. $start_time = date('Y-m-d H:i:s',$request->input('start_time'));
  725. $end_time = date('Y-m-d H:i:s',$request->input('end_time'));//dd($start_time.';'.$end_time);
  726. $query = $query->where([['created_at','>=',$start_time],['created_at','<=',$end_time]]);
  727. break;
  728. case '3':
  729. if(!$request->input('action')) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!');
  730. $query = $query->where([['action',$request->input('action')]]);
  731. break;
  732. }
  733. $res = $query->get();
  734. $action = '';
  735. foreach($res as $key=>$val){
  736. \Log::info('action:'.$val->action.',id:'.$val->id.';end');
  737. if($val->action==4){
  738. $action = '进入我的画册';
  739. }elseif($val->action == 1||$val->action ==3){
  740. $goods_data = json_decode($val->detail,true);
  741. $goods_id = $goods_data['goods_id'];
  742. $goods = AlbumProductModel::find($goods_id);
  743. $cat = AlbumCatModel::find($goods->cat_id);
  744. $style = AlbumProductStyleModel::find($goods->style);
  745. $val->action == 1? $act = '收藏':$act = '查看';if(empty($style)||empty($cat)) continue;
  746. $action = $act.'了您的'.$cat->name.'分类下的'.$style->name.'风格的商品 '.$goods->name;
  747. }elseif($val->action == 2){
  748. //dd(111);
  749. $cat_data = json_decode($val->detail,true);
  750. if($cat_data['cat_id']=="undefined"||$cat_data['style']=="undefined"||!$cat_data['style']||!$cat_data['cat_id']) continue;
  751. $cat = AlbumCatModel::find($cat_data['cat_id']);
  752. $style = AlbumProductStyleModel::find($cat_data['style']);
  753. if(empty($style)||empty($cat)) continue;
  754. $action = '浏览了您的 '.$cat->name.'分类下的 '.$style->name.'风格的商品列表';
  755. }elseif($val->action==5){
  756. $goods_data = json_decode($val->detail,true);
  757. $goods_id = $goods_data['goods_id'];
  758. $goods = AlbumProductModel::find($goods_id);
  759. $cat = AlbumCatModel::find($goods->cat_id);
  760. $style = AlbumProductStyleModel::find($goods->style);if(empty($style)||empty($cat)) continue;
  761. $action = '点击了您的'.$cat->name.'分类下的'.$style->name.'风格的商品 '.$goods->name.'的'.$goods_data['detail'];
  762. }elseif($val->action==6){
  763. $detail = json_decode($val->detail,true);
  764. $address = $detail['detail'];
  765. $action = '查看了您的地址'.$address.',并且点击了导航';
  766. }elseif($val->action==7){
  767. $detail = json_decode($val->detail,true);
  768. $phone = $detail['detail'];
  769. $action = '查看了您的电话'.$phone.',并且点击了拨号';
  770. }
  771. $user = AlbumUserModel::where([['wechat_open_id',$val->open_id],['store_id',$val->store_id]])->select(['username','avatar','phone'])->first();
  772. // dd(111);
  773. $data_res[] = [
  774. 'time'=>$val->created_at,
  775. 'action'=>$action,
  776. 'user'=>$user
  777. ];
  778. }
  779. return $this->api(compact('data_res'));
  780. }
  781. /**
  782. * @api {post} /api/album/get_data_goods 获取访问数据(get_data_goods)
  783. * @apiDescription 获取访问数据(get_data_goods)
  784. * @apiGroup Album
  785. * @apiPermission 需要登录
  786. * @apiVersion 0.1.0
  787. * @apiParam {int} [store_id] 商户id 模拟值为0
  788. * @apiSuccessExample {json} Success-Response:
  789. * HTTP/1.1 200 OK
  790. * {
  791. * "status": true,
  792. * "status_code": 0,
  793. * "message": "",
  794. * "data": [
  795. * {
  796. * "1": [
  797. * 'name':'asdawd',
  798. * 'point':'asdawd',
  799. * 'num':'1',
  800. * ],
  801. * }
  802. * ]
  803. * }
  804. * @apiErrorExample {json} Error-Response:
  805. * HTTP/1.1 400 Bad Request
  806. * {
  807. * "state": false,
  808. * "code": 1000,
  809. * "message": "传入参数不正确",
  810. * "data": null or []
  811. * }
  812. * 可能出现的错误代码:
  813. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  814. */
  815. public function albumGetDataGoods(Request $request)
  816. {
  817. $userAuth = Auth('api')->user();
  818. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  819. $validator = Validator::make($request->all(), [
  820. 'store_id' => 'required'
  821. ],[
  822. 'store_id.required'=>'缺少商户参数'
  823. ]);
  824. if ($validator->fails()) {
  825. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  826. }
  827. $store_id = $request->input('store_id');
  828. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  829. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  830. $res = AlbumWatchRecord::where([['agent_id',$user_agent->id],['store_id',$store_id],['action',3]])->get();
  831. $arr = array();
  832. $total = 0;
  833. foreach($res as $key=>$val){
  834. $goods_data = json_decode($val->detail,true);
  835. $goods_id = $goods_data['goods_id'];
  836. $goods = AlbumProductModel::find($goods_id);
  837. if(isset($arr[$goods->id])){
  838. $arr[$goods->id]['num']++;
  839. }else{
  840. $arr[$goods->id]['num']=1;
  841. $arr[$goods->id]['name']=$goods->name;
  842. }
  843. $total++;
  844. }
  845. foreach($arr as $key=>$val){
  846. $arr[$key]['point'] = ($val['num']/$total*100).'%';
  847. }
  848. return $this->api(compact('arr'));
  849. }
  850. /**
  851. * @api {post} /api/album/get_data_cat 获取分类访问数据(get_data_cat)
  852. * @apiDescription 获取分类访问数据(get_data_cat)
  853. * @apiGroup Album
  854. * @apiPermission 需要登录
  855. * @apiVersion 0.1.0
  856. * @apiParam {int} [store_id] 商户id 模拟值为0
  857. * @apiSuccessExample {json} Success-Response:
  858. * HTTP/1.1 200 OK
  859. * {
  860. * "status": true,
  861. * "status_code": 0,
  862. * "message": "",
  863. * "data": [
  864. * {
  865. * "1": [
  866. * 'name':'asdawd',
  867. * 'point':'asdawd',
  868. * 'num':'1',
  869. * ],
  870. * }
  871. * ]
  872. * }
  873. * @apiErrorExample {json} Error-Response:
  874. * HTTP/1.1 400 Bad Request
  875. * {
  876. * "state": false,
  877. * "code": 1000,
  878. * "message": "传入参数不正确",
  879. * "data": null or []
  880. * }
  881. * 可能出现的错误代码:
  882. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  883. */
  884. public function albumGetDataCat(Request $request)
  885. {
  886. $userAuth = Auth('api')->user();
  887. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  888. $validator = Validator::make($request->all(), [
  889. 'store_id' => 'required',
  890. 'style_id' => 'required'
  891. ],[
  892. 'store_id.required'=>'缺少商户参数',
  893. 'style_id.required'=>'缺少风格参数',
  894. ]);
  895. if ($validator->fails()) {
  896. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  897. }
  898. $store_id = $request->input('store_id');
  899. $style_id = $request->input('style_id');
  900. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  901. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  902. $cat = AlbumCatModel::where('store_id',$store_id)->get(['name','id'])->toArray();
  903. $total = 0;
  904. foreach ($cat as $key=>$val){
  905. $count = CustomerCatRecordModel::where([['agent_id',$user_agent->id],['store_id',$store_id],['style_id',$style_id],['cat_id',$val['id']]])->count();
  906. $total += $count;
  907. $cat[$key]['num'] = $count;
  908. }
  909. foreach($cat as $key=>$val){
  910. if($val['num']==0||$total==0){
  911. $cat[$key]['point']=0;
  912. }else{
  913. $cat[$key]['point'] = ($val['num']/$total*100).'%';
  914. }
  915. }
  916. return $this->api(compact('cat'));
  917. }
  918. /**
  919. * @api {post} /api/album/get-cart-of-watch 客户浏览情况概览(get-cart-of-watch)
  920. * @apiDescription 客户浏览情况概览(get-cart-of-watch)
  921. * @apiGroup Album
  922. * @apiPermission none
  923. * @apiVersion 0.1.0
  924. * @apiParam {int} [store_id] 商户id 模拟值为0
  925. * @apiSuccessExample {json} Success-Response:
  926. * HTTP/1.1 200 OK
  927. * {
  928. * "status": true,
  929. * "status_code": 0,
  930. * "message": "",
  931. * "data": [
  932. * "user": {
  933. * "username":"王小贱",
  934. * "avatar":"awdawdawdawdawdawd",头像
  935. * "phone":"1123123123123"
  936. * "wechat_open_id":"1123123123123"
  937. * }
  938. * "count":6,
  939. * "action":[
  940. * "type1":1,
  941. * "type2":1,
  942. * "type3":1,
  943. * "type4":1,
  944. * "type5":1,
  945. * "type6":1,
  946. * "type7":1,
  947. * ]
  948. * ]
  949. * }
  950. * @apiErrorExample {json} Error-Response:
  951. * HTTP/1.1 400 Bad Request
  952. * {
  953. * "state": false,
  954. * "code": 1000,
  955. * "message": "传入参数不正确",
  956. * "data": null or []
  957. * }
  958. * 可能出现的错误代码:
  959. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  960. */
  961. public function albumGetCartOfWatch(Request $request)
  962. {
  963. $userAuth = Auth('api')->user();
  964. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  965. $validator = Validator::make($request->all(), [
  966. 'store_id' => 'required',
  967. ],[
  968. 'store_id.required'=>'缺少商户参数',
  969. ]);
  970. if ($validator->fails()) {
  971. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  972. }
  973. $store_id = $request->input('store_id');
  974. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  975. // if($userAuth->up_agent_id==0) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未选择经销商!', $validator->messages());
  976. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  977. $count_user = AlbumWatchRecord::where([['agent_id',$user_agent->id],['store_id',$store_id]])->groupBy('open_id')->get();
  978. $users = Array();
  979. if($count_user){
  980. foreach($count_user as $key=>$val){
  981. $user = AlbumUserModel::where([['wechat_open_id',$val->open_id],['store_id',$val->store_id]])->select(['id','username','avatar','phone','wechat_open_id'])->first();
  982. $customer = CustomerDetailsModel::where([['store_id',$store_id],['open_id',$val->open_id],['agent_id',$user_agent->id]])->first(['purpose_level','comment','tips']);
  983. if($customer){
  984. $user->purpose_level = $customer->purpose_level;
  985. $user->comment = $customer->comment;
  986. $user->tips = $customer->tips;
  987. }else{
  988. $user->purpose_level = 0;
  989. $user->comment = 0;
  990. $user->tips = 0;
  991. }
  992. $users[] = $user;
  993. }
  994. }
  995. $count = count($users);
  996. $action = Array();
  997. for($i=1;$i<=7;$i++){
  998. $res = AlbumWatchRecord::where([['agent_id',$user_agent->id],['store_id',$store_id],['action',$i]])->get();
  999. $action['type'.$i] = count($res);
  1000. }
  1001. return $this->api(compact('count','users','action'));
  1002. }
  1003. /**
  1004. * @api {get} /api/album/checklogin 登陆应用(checklogin)
  1005. * @apiDescription 登陆应用(checklogin)
  1006. * @apiGroup Album
  1007. * @apiPermission none
  1008. * @apiVersion 0.1.0
  1009. * @apiParam {int} [store_id] 商户id 模拟值为0
  1010. * @apiSuccessExample {json} Success-Response:
  1011. * HTTP/1.1 200 OK
  1012. * {
  1013. * "status": true,
  1014. * "status_code": 0,
  1015. * "message": "",
  1016. * "data": {
  1017. *
  1018. * }
  1019. * }
  1020. * @apiErrorExample {json} Error-Response:
  1021. * HTTP/1.1 400 Bad Request
  1022. * {
  1023. * "state": false,
  1024. * "code": 1000,
  1025. * "message": "传入参数不正确",
  1026. * "data": null or []
  1027. * }
  1028. * 可能出现的错误代码:
  1029. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1030. */
  1031. public function albumchecklogin(Request $request)
  1032. {
  1033. $validator = Validator::make($request->all(), [
  1034. 'store_id' => 'required',
  1035. ],[
  1036. 'store_id.required'=>'缺少商户参数',
  1037. ]);
  1038. if ($validator->fails()) {
  1039. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1040. }
  1041. $data = $request->input();
  1042. $favorite = AlbumManufacturerModel::where([['store_id',$data['store_id']]])->first();
  1043. $conf = $favorite->redirectapp;
  1044. return $this->api($conf);
  1045. }
  1046. /**
  1047. * @api {get} /api/album/favorite_del 收藏删除(favorite_del)
  1048. * @apiDescription 收藏删除(favorite_del)
  1049. * @apiGroup Album
  1050. * @apiPermission none
  1051. * @apiVersion 0.1.0
  1052. * @apiParam {int} [store_id] 商户id 模拟值为0
  1053. * @apiParam {int} [favorite_id] 收藏id
  1054. * @apiSuccessExample {json} Success-Response:
  1055. * HTTP/1.1 200 OK
  1056. * {
  1057. * "status": true,
  1058. * "status_code": 0,
  1059. * "message": "",
  1060. * "data":[]//返回状态
  1061. * }
  1062. * @apiErrorExample {json} Error-Response:
  1063. * HTTP/1.1 400 Bad Request
  1064. * {
  1065. * "state": false,
  1066. * "code": 1000,
  1067. * "message": "传入参数不正确",
  1068. * "data": null or []
  1069. * }
  1070. * 可能出现的错误代码:
  1071. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1072. */
  1073. public function albumFavoriteDel(Request $request)
  1074. {
  1075. //$userAuth = AlbumXyxUserModel::find(1);
  1076. $userAuth = Auth('api')->user();
  1077. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1078. $validator = Validator::make($request->all(), [
  1079. 'store_id' => 'required',
  1080. 'favorite_id' => 'required',
  1081. ],[
  1082. 'store_id.required'=>'缺少商户参数',
  1083. 'favorite_id.required'=>'缺少收藏参数',
  1084. ]);
  1085. if ($validator->fails()) {
  1086. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1087. }
  1088. $data = $request->input();
  1089. $favorite = AlbumFavoriteModel::find($data['favorite_id']);
  1090. if(!$favorite){
  1091. $d = [
  1092. 'code' =>1,
  1093. 'msg' =>'删除失败,或者该收藏已移除',
  1094. ];
  1095. } else {
  1096. $res = $favorite->delete();
  1097. if($res){
  1098. $d = [
  1099. 'code' =>1,
  1100. 'msg' =>'删除成功',
  1101. ];
  1102. } else {
  1103. $d = [
  1104. 'code' =>1,
  1105. 'msg' =>'删除失败,或者该收藏已移除',
  1106. ];
  1107. }
  1108. }
  1109. return $this->api($d);
  1110. }
  1111. /**
  1112. * @api {get} /api/album/save_form_id 添加Form_id(save_form_id)
  1113. * @apiDescription 添加Form_id(save_form_id)
  1114. * @apiGroup Album
  1115. * @apiPermission none
  1116. * @apiVersion 0.1.0
  1117. * @apiParam {int} [store_id] 商户id
  1118. * @apiParam {int} [form_id] form_id
  1119. * @apiSuccessExample {json} Success-Response:
  1120. * HTTP/1.1 200 OK
  1121. * {
  1122. * "status": true,
  1123. * "status_code": 0,
  1124. * "message": "",
  1125. * "data": []//返回信息
  1126. * }
  1127. * @apiErrorExample {json} Error-Response:
  1128. * HTTP/1.1 400 Bad Request
  1129. * {
  1130. * "state": false,
  1131. * "code": 1000,
  1132. * "message": "传入参数不正确",
  1133. * "data": null or []
  1134. * }
  1135. * 可能出现的错误代码:
  1136. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1137. */
  1138. public function albumSaveFormId(Request $request)
  1139. {
  1140. $userAuth = Auth('api')->user();
  1141. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1142. $validator = Validator::make($request->all(), [
  1143. 'store_id' => 'required',
  1144. 'form_id' => 'required',
  1145. ],[
  1146. 'store_id.required'=>'缺少商户参数',
  1147. 'form_id.required'=>'缺少FormId',
  1148. ]);
  1149. if ($validator->fails()) {
  1150. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1151. }
  1152. $data = $request->input();
  1153. $data['open_id'] = $userAuth->wechat_open_id;
  1154. $res = AlbumFormId::create($data);
  1155. if($res){
  1156. $d = [
  1157. 'code' =>0,
  1158. 'msg' =>'success',
  1159. ];
  1160. }else{
  1161. $d = [
  1162. 'code' =>1,
  1163. 'msg' =>'error',
  1164. ];
  1165. }
  1166. return $this->api($d);
  1167. }
  1168. /**
  1169. * @api {get} /api/album/add_favorite 添加收藏(add_favorite)
  1170. * @apiDescription 添加收藏(favorite_list)
  1171. * @apiGroup Album
  1172. * @apiPermission none
  1173. * @apiVersion 0.1.0
  1174. * @apiParam {int} [store_id] 商户id
  1175. * @apiParam {int} [product_id] 商品id
  1176. * @apiSuccessExample {json} Success-Response:
  1177. * HTTP/1.1 200 OK
  1178. * {
  1179. * "status": true,
  1180. * "status_code": 0,
  1181. * "message": "",
  1182. * "data": []//返回信息
  1183. * }
  1184. * @apiErrorExample {json} Error-Response:
  1185. * HTTP/1.1 400 Bad Request
  1186. * {
  1187. * "state": false,
  1188. * "code": 1000,
  1189. * "message": "传入参数不正确",
  1190. * "data": null or []
  1191. * }
  1192. * 可能出现的错误代码:
  1193. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1194. */
  1195. public function albumAddFavorite(Request $request)
  1196. {
  1197. $userAuth = Auth('api')->user();
  1198. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1199. $validator = Validator::make($request->all(), [
  1200. 'store_id' => 'required',
  1201. 'product_id' => 'required',
  1202. ],[
  1203. 'store_id.required'=>'缺少商户参数',
  1204. 'product_id.required'=>'缺少商品参数',
  1205. ]);
  1206. if ($validator->fails()) {
  1207. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1208. }
  1209. $data = $request->input();
  1210. $select_info = ['goods_id'=>$data['product_id']];
  1211. if($userAuth->up_agent_id!=0){
  1212. $add_record['agent_id'] = $userAuth->up_agent_id;
  1213. $add_record['open_id'] = $userAuth->wechat_open_id;
  1214. $add_record['action'] = 1;
  1215. $add_record['store_id'] = $data['store_id'];
  1216. $add_record['detail'] = json_encode($select_info);
  1217. $user_agent = AlbumAgentModel::where('id',$userAuth->up_agent_id)->first();
  1218. $agent = AlbumUserModel::where('id',$user_agent->user_id)->first();
  1219. try{
  1220. $this->sendLogsMessage($data['store_id'],$agent->wechat_open_id,1,$userAuth->username);
  1221. }catch (\Exception $e){
  1222. }
  1223. AlbumWatchRecord::create($add_record);
  1224. }
  1225. $check_user = AlbumUserModel::find($userAuth->id);
  1226. $check_f = AlbumFavoriteModel::where([['user_id',$userAuth->id],['product_id',$data['product_id']]])->first();
  1227. if(!$check_user||$check_f){
  1228. $d = [
  1229. 'code' =>1,
  1230. 'msg' =>'该用户不存在或者已添加',
  1231. ];
  1232. return $this->api($d);
  1233. }
  1234. $data['user_id'] = $userAuth->id;
  1235. $res = AlbumFavoriteModel::create($data);
  1236. if($res){
  1237. $d = [
  1238. 'code' =>0,
  1239. 'id'=>$res->id,
  1240. 'msg' =>'success',
  1241. ];
  1242. }else{
  1243. $d = [
  1244. 'code' =>1,
  1245. 'msg' =>'error',
  1246. ];
  1247. }
  1248. return $this->api($d);
  1249. }
  1250. /**
  1251. * @api {get} /api/album/cat 分类列表(cat)
  1252. * @apiDescription 画册分类(cat)
  1253. * @apiGroup Album
  1254. * @apiPermission none
  1255. * @apiVersion 0.1.0
  1256. * @apiParam {int} [parent_id] 一级菜单传0 二级菜单的id从一级菜单里面获取
  1257. * @apiParam {int} [store_id] 商户id 模拟值为0
  1258. * @apiSuccessExample {json} Success-Response:
  1259. * HTTP/1.1 200 OK
  1260. * {
  1261. * "status": true,
  1262. * "status_code": 0,
  1263. * "message": "",
  1264. * "data": [
  1265. * {
  1266. * "id": 9,
  1267. * "name": "圣地亚哥",
  1268. * "parent_id": 0,
  1269. * "level": "00",
  1270. * "pic_url": "http://admin.xcx.com/upload/images/20180518/af6cc8fd71241744ccd638afc6ac25f2.png",
  1271. * "created_at": "2018-05-19 14:55:48",
  1272. * "updated_at": "2018-05-19 14:55:48",
  1273. * "store_id": 0,
  1274. * "deleted_at": null,
  1275. * "sort": 0
  1276. * }
  1277. * ]
  1278. * }
  1279. * @apiErrorExample {json} Error-Response:
  1280. * HTTP/1.1 400 Bad Request
  1281. * {
  1282. * "state": false,
  1283. * "code": 1000,
  1284. * "message": "传入参数不正确",
  1285. * "data": null or []
  1286. * }
  1287. * 可能出现的错误代码:
  1288. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1289. */
  1290. public function albumCat(Request $request)
  1291. {
  1292. $validator = Validator::make($request->all(), [
  1293. 'store_id' => 'required',
  1294. ],[
  1295. 'store_id.required'=>'缺少商户参数',
  1296. ]);
  1297. if ($validator->fails()) {
  1298. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1299. }
  1300. $cat_id = 0;
  1301. $store_id = request('store_id');
  1302. $cats = AlbumCatModel::where([['parent_id',$cat_id],['store_id',$store_id]])->orderByDesc('sort')->get();
  1303. return $this->api($cats);
  1304. }
  1305. /**
  1306. * @api {get} /api/album/style 属性列表(style)
  1307. * @apiDescription 分类样式(style)
  1308. * @apiGroup Album
  1309. * @apiPermission none
  1310. * @apiVersion 0.1.0
  1311. * @apiParam {int} [store_id] 商户id 模拟值为0
  1312. * @apiSuccessExample {json} Success-Response:
  1313. * HTTP/1.1 200 OK
  1314. * {
  1315. * "status": true,
  1316. * "status_code": 0,
  1317. * "message": "",
  1318. * "data": [
  1319. * {
  1320. * "id": 2,
  1321. * "name": "1111",
  1322. * "deleted_at": null,
  1323. * "created_at": "2018-06-05 17:35:08",
  1324. * "updated_at": "2018-06-05 17:35:08",
  1325. * "store_id": 0
  1326. * }
  1327. * ]
  1328. * }
  1329. * @apiErrorExample {json} Error-Response:
  1330. * HTTP/1.1 400 Bad Request
  1331. * {
  1332. * "state": false,
  1333. * "code": 1000,
  1334. * "message": "传入参数不正确",
  1335. * "data": null or []
  1336. * }
  1337. * 可能出现的错误代码:
  1338. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1339. */
  1340. public function albumStyle(Request $request)
  1341. {
  1342. $validator = Validator::make($request->all(), [
  1343. 'store_id' => 'required',
  1344. ],[
  1345. 'store_id.required'=>'缺少商户参数',
  1346. ]);
  1347. if ($validator->fails()) {
  1348. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1349. }
  1350. $store_id = request('store_id');
  1351. $style = AlbumProductStyleModel::where([['store_id',$store_id]])->orderByDesc('id')->get();
  1352. return $this->api($style);
  1353. }
  1354. /**
  1355. * @api {get} /api/album/set-price 经销商修改价格(set-price)
  1356. * @apiDescription 画册(set-price)
  1357. * @apiGroup Album
  1358. * @apiPermission none
  1359. * @apiVersion 0.1.0
  1360. * @apiParam {int} [goods_id] 商品id
  1361. * @apiParam {int} [store_id] 商户id 模拟值为0
  1362. * @apiParam {double} [price] 价格
  1363. * @apiSuccessExample {json} Success-Response:
  1364. * HTTP/1.1 200 OK
  1365. * {
  1366. * "status": true,
  1367. * "status_code": 0,
  1368. * "message": "",
  1369. * "data":[
  1370. * "msg":返回信息,
  1371. * "data":修改后价格
  1372. * ]
  1373. * }
  1374. * @apiErrorExample {json} Error-Response:
  1375. * HTTP/1.1 400 Bad Request
  1376. * {
  1377. * "state": false,
  1378. * "code": 1000,
  1379. * "message": "传入参数不正确",
  1380. * "data": null or []
  1381. * }
  1382. * 可能出现的错误代码:
  1383. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1384. */
  1385. public function albumSetPrice(Request $request)
  1386. {
  1387. $userAuth = Auth('api')->user();
  1388. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1389. $agent_id = $user_agent['id'];
  1390. $data = $request->all();
  1391. //print_r($data);
  1392. $save = array();
  1393. $return_data = array();
  1394. $save['price'] = $data['price'];
  1395. $check = AlbumProductPriceModel::where([['agent_id',$agent_id],['store_id',$data['store_id']],['product_id',$data['goods_id']]])->first();
  1396. if(empty($check)){
  1397. $save['agent_id'] = $agent_id;
  1398. $save['store_id'] = $data['store_id'];
  1399. $save['product_id'] = $data['goods_id'];
  1400. $res = AlbumProductPriceModel::create($save);
  1401. }else{
  1402. $res = AlbumProductPriceModel::where([['agent_id',$agent_id],['store_id',$data['store_id']],['product_id',$data['goods_id']]])->update($save);
  1403. }
  1404. if($res){
  1405. $return['msg'] = 'success';
  1406. $return_data['price'] = $data['price'];
  1407. } else {
  1408. $return['msg'] = 'error';
  1409. }
  1410. $return['data'] = $return_data;
  1411. return $this->api($return);
  1412. }
  1413. /**
  1414. * @api {post} /api/album/set-phone 获取用户号码(set-phone)
  1415. * @apiDescription 获取用户号码(set-phone)
  1416. * @apiGroup Album
  1417. * @apiPermission none
  1418. * @apiVersion 0.1.0
  1419. * @apiParam {int} [store_id] 商户id 模拟值为0
  1420. * @apiParam {string} [code] code
  1421. * @apiParam {string} [iv] 偏移量
  1422. * @apiParam {string} [encrypted] 加密参数
  1423. * @apiSuccessExample {json} Success-Response:
  1424. * HTTP/1.1 200 OK
  1425. * {
  1426. * "status": true,
  1427. * "status_code": 0,
  1428. * "message": "",
  1429. * "data":[
  1430. * "msg":返回信息,
  1431. * ]
  1432. * }
  1433. * @apiErrorExample {json} Error-Response:
  1434. * HTTP/1.1 400 Bad Request
  1435. * {
  1436. * "state": false,
  1437. * "code": 1000,
  1438. * "message": "传入参数不正确",
  1439. * "data": null or []
  1440. * }
  1441. * 可能出现的错误代码:
  1442. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1443. */
  1444. public function albumSavePhone(Request $request)
  1445. {
  1446. $userAuth = Auth('api')->user();
  1447. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1448. $validator = Validator::make($request->all(), [
  1449. 'store_id' => 'required',
  1450. 'code' => 'required',
  1451. 'iv' => 'required',
  1452. 'encrypted' => 'required',
  1453. ],[
  1454. 'store_id.required'=>'缺少商户参数',
  1455. 'code.required'=>'缺少code参数',
  1456. 'iv.required'=>'缺少偏移量参数',
  1457. 'encrypted.required'=>'缺少加密参数',
  1458. ]);
  1459. if ($validator->fails()) {
  1460. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1461. }
  1462. $this->wechat_app = AlbumManufacturerModel::where('store_id',$request->input('store_id'))->first();
  1463. $config = [
  1464. 'app_id' => $this->wechat_app->xyx_id,
  1465. 'secret' => $this->wechat_app->xyx_secret,
  1466. // 下面为可选项
  1467. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  1468. 'response_type' => 'array',
  1469. ];
  1470. $app = Factory::miniProgram($config);
  1471. $res = $app->auth->session($request->input('code'));
  1472. if (!$res || empty($res['openid'])) {
  1473. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '获取用户OpenId失败!', $validator->messages());
  1474. }
  1475. $session_key = $res['session_key'];
  1476. $pc = new WXBizDataCrypt($this->wechat_app->xyx_id, $session_key);
  1477. $errCode = $pc->decryptData($request->input('encrypted'), $request->input('iv'), $data);
  1478. if($errCode==0){
  1479. $data = json_decode($data,true);
  1480. $userAuth->phone = $data['phoneNumber'];
  1481. $res = $userAuth->save();
  1482. if($res){
  1483. return $this->api(['msg'=>'success']);
  1484. }else{
  1485. return $this->api(['msg'=>'error']);
  1486. }
  1487. }elseif($errCode == -41003){
  1488. return $this->api(['msg'=>'请求错误,请重试']);
  1489. }
  1490. }
  1491. /**
  1492. * @api {post} /api/album/set-watch 获取用户操作记录(set-watch)
  1493. * @apiDescription 获取用户操作记录(set-watch)
  1494. * @apiGroup Album
  1495. * @apiPermission none
  1496. * @apiVersion 0.1.0
  1497. * @apiParam {int} [store_id] 商户id 模拟值为0
  1498. * @apiParam {string} [detail] 详细
  1499. * @apiParam {string} [type] 5点击图片 6 点击导航 7一键拨号
  1500. * @apiSuccessExample {json} Success-Response:
  1501. * HTTP/1.1 200 OK
  1502. * {
  1503. * "status": true,
  1504. * "status_code": 0,
  1505. * "message": "",
  1506. * "data":[
  1507. * "msg":返回信息,
  1508. * ]
  1509. * }
  1510. * @apiErrorExample {json} Error-Response:
  1511. * HTTP/1.1 400 Bad Request
  1512. * {
  1513. * "state": false,
  1514. * "code": 1000,
  1515. * "message": "传入参数不正确",
  1516. * "data": null or []
  1517. * }
  1518. * 可能出现的错误代码:
  1519. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1520. */
  1521. public function albumSetWatch(Request $request)
  1522. {
  1523. $userAuth = Auth('api')->user();
  1524. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1525. $validator = Validator::make($request->all(), [
  1526. 'store_id' => 'required',
  1527. 'type' => 'required',
  1528. 'detail' => 'required',
  1529. ],[
  1530. 'store_id.required'=>'缺少商户参数',
  1531. 'type.required'=>'缺少类型参数',
  1532. 'detail.required'=>'缺少详细参数',
  1533. ]);
  1534. if ($validator->fails()) {
  1535. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1536. }
  1537. $data = $request->input();
  1538. if($data['type']==5&&!empty($data['goods_id'])) {
  1539. $select_info = ['goods_id'=>$data['goods_id'],'detail'=>$data['detail']];
  1540. }elseif($data['type']==5&&empty($data['goods_id'])){
  1541. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '缺少goodsid');
  1542. }else{
  1543. $select_info = ['detail'=>$data['detail']];
  1544. }
  1545. if($userAuth->up_agent_id!=0){
  1546. $add_record['agent_id'] = $userAuth->up_agent_id;
  1547. $add_record['open_id'] = $userAuth->wechat_open_id;
  1548. $add_record['action'] = $data['type'];
  1549. $add_record['store_id'] = 1;
  1550. $add_record['detail'] = json_encode($select_info);
  1551. // dd($add_record);
  1552. $res = AlbumWatchRecord::create($add_record);
  1553. $user_agent = AlbumAgentModel::where('id',$userAuth->up_agent_id)->first();
  1554. $agent = AlbumUserModel::where('id',$user_agent->user_id)->first();
  1555. if($res){
  1556. try{
  1557. $this->sendLogsMessage($data['store_id'],$agent->wechat_open_id,$data['type'],$userAuth->username);
  1558. }catch (\Exception $e){
  1559. }
  1560. return $this->api(['msg'=>'success']);
  1561. }else{
  1562. return $this->api(['msg'=>'error']);
  1563. }
  1564. }
  1565. }
  1566. /**
  1567. * @param $storeid
  1568. * @param $open_id
  1569. * @param $action 1 收藏 2查看类目 3查看商品 4登陆 5点击图片 6 点击导航 7一键拨号
  1570. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  1571. */
  1572. private function sendLogsMessage($storeid,$open_id,$action,$name)
  1573. {
  1574. $user = AlbumUserModel::where('wechat_open_id',$open_id)->first();
  1575. $formId = AlbumFormId::where([['open_id',$open_id]])->first();
  1576. if($user&&$formId){
  1577. $content = '';
  1578. switch ($action){
  1579. case 1:
  1580. $content = '有用户收藏了您的产品';
  1581. break;
  1582. case 2;
  1583. $content = '有用户查看了您的产品列表';
  1584. break;
  1585. case 3;
  1586. $content = '有用户查看了您的商品';
  1587. break;
  1588. case 4;
  1589. $content = '有用户进入了您的画册';
  1590. break;
  1591. case 5;
  1592. $content = '有用户点击了您的某款产品详情';
  1593. break;
  1594. case 6;
  1595. $content = '有用户点击了导航到您的位置';
  1596. break;
  1597. case 7;
  1598. $content = '有用户点击了您的号码';
  1599. break;
  1600. }
  1601. $this->wechat_app = AlbumManufacturerModel::where('store_id',$storeid)->first();
  1602. $config = [
  1603. 'app_id' => $this->wechat_app->app_id,
  1604. 'secret' => $this->wechat_app->app_secret,
  1605. // 下面为可选项
  1606. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  1607. 'response_type' => 'array',
  1608. ];
  1609. $app = Factory::miniProgram($config);
  1610. $res = $app->template_message->send([
  1611. 'touser' => $open_id,
  1612. 'template_id' => '9PDTfnhsRvdNwpPMIcYLL7wQF4SZkB8-9ZhXUfthbmw',
  1613. 'page' => 'pages/logs/logs',
  1614. 'form_id' => $formId->form_id,
  1615. 'data' => [
  1616. 'keyword1' => $name,
  1617. 'keyword2' => $content,
  1618. 'keyword3' => date('Y-m-d H:i'),
  1619. ],
  1620. ]);
  1621. // dd($res);
  1622. if($res){
  1623. $formId->delete();
  1624. }
  1625. }
  1626. }
  1627. /**
  1628. * @api {get} /api/album/add_agent 申请经销商(add_agent)
  1629. * @apiDescription 申请经销商(add_agent)
  1630. * @apiGroup Album
  1631. * @apiPermission none
  1632. * @apiVersion 0.1.0
  1633. * @apiParam {int} [store_id] 商户id
  1634. * @apiParam {int} [realname] 姓名
  1635. * @apiParam {int} [address] 地址
  1636. * @apiParam {double} [lat] lat
  1637. * @apiParam {double} [lon] lon
  1638. * @apiParam {int} [phone] 电话
  1639. * @apiSuccessExample {json} Success-Response:
  1640. * HTTP/1.1 200 OK
  1641. * {
  1642. * "status": true,
  1643. * "status_code": 0,
  1644. * "message": "",
  1645. * "data": []//返回信息
  1646. * }
  1647. * @apiErrorExample {json} Error-Response:
  1648. * HTTP/1.1 400 Bad Request
  1649. * {
  1650. * "state": false,
  1651. * "code": 1000,
  1652. * "message": "传入参数不正确",
  1653. * "data": null or []
  1654. * }
  1655. * 可能出现的错误代码:
  1656. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1657. */
  1658. public function albumAddAgent(Request $request)
  1659. {
  1660. $userAuth = Auth('api')->user();
  1661. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1662. $validator = Validator::make($request->all(), [
  1663. 'realname' => 'required',
  1664. 'address' => 'required',
  1665. 'lon' => 'required',
  1666. 'lat' => 'required',
  1667. 'phone' => 'required',
  1668. 'store_id' => 'required',
  1669. ],[
  1670. 'address.required'=>'缺少地址参数',
  1671. 'lon.required'=>'缺少lon参数',
  1672. 'lat.required'=>'缺少lat参数',
  1673. 'real_name.required'=>'缺少信息参数',
  1674. 'phone.required'=>'缺少电话参数',
  1675. 'store_id.required'=>'缺少商户参数',
  1676. ]);
  1677. if ($validator->fails()) {
  1678. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1679. }
  1680. $data = $request->input();
  1681. $agent_check = AlbumAgentModel::where([['store_id',$data['store_id']],['user_id',$userAuth->id]])->first();
  1682. if($agent_check){
  1683. if($agent_check->status==0) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户已提交申请!');
  1684. $agent_check->phone = $data['phone'];
  1685. $agent_check->address = $data['address'];
  1686. $agent_check->lon = $data['lon'];
  1687. $agent_check->lat = $data['lat'];
  1688. $agent_check->realname = $data['realname'];
  1689. if($agent_check->save()) return $this->api([
  1690. 'code' =>0,
  1691. 'msg' =>'success',
  1692. ]);
  1693. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '保存失败!');
  1694. }
  1695. $data['status'] = 0;
  1696. $data['user_id'] = $userAuth->id;
  1697. $data['name'] = $userAuth->username;
  1698. $res = AlbumAgentModel::create($data);
  1699. if($res){
  1700. $d = [
  1701. 'code' =>0,
  1702. 'msg' =>'success',
  1703. ];
  1704. }else{
  1705. $d = [
  1706. 'code' =>1,
  1707. 'msg' =>'error',
  1708. ];
  1709. }
  1710. return $this->api($d);
  1711. }
  1712. /**
  1713. * @api {get} /api/album/get-agent-address 获取经销商地址(get-agent-address)
  1714. * @apiDescription 获取经销商地址(get-agent-address)
  1715. * @apiGroup Album
  1716. * @apiPermission none
  1717. * @apiVersion 0.1.0
  1718. * @apiSuccessExample {json} Success-Response:
  1719. * HTTP/1.1 200 OK
  1720. * {
  1721. * "status": true,
  1722. * "status_code": 0,
  1723. * "message": "",
  1724. * "data":[
  1725. * "realname":
  1726. * "phone":
  1727. * "address":
  1728. * ]
  1729. * }
  1730. * @apiErrorExample {json} Error-Response:
  1731. * HTTP/1.1 400 Bad Request
  1732. * {
  1733. * "state": false,
  1734. * "code": 1000,
  1735. * "message": "传入参数不正确",
  1736. * "data": null or []
  1737. * }
  1738. * 可能出现的错误代码:
  1739. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1740. */
  1741. public function albumGetAgentAdress(Request $request)
  1742. {
  1743. $userAuth = Auth('api')->user();
  1744. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1745. $validator = Validator::make($request->all(), [
  1746. 'store_id' => 'required',
  1747. ],[
  1748. 'store_id.required'=>'缺少商户参数',
  1749. ]);
  1750. if ($validator->fails()) {
  1751. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1752. }
  1753. $data = $request->input();
  1754. if($userAuth->is_dealer == 1){
  1755. $user_agent = AlbumAgentModel::where([['store_id',$data['store_id']],['user_id',$userAuth->id]])->first(['address','phone','realname']);
  1756. }else{
  1757. $user_agent = null;
  1758. }
  1759. return $this->api(compact('user_agent'));
  1760. }
  1761. /**
  1762. * @api {get} /api/album/get-banner 获取banner和視頻(get-banner)
  1763. * @apiDescription 获取banner和視頻(get-banner)
  1764. * @apiGroup Album
  1765. * @apiPermission none
  1766. * @apiVersion 0.1.0
  1767. * @apiSuccessExample {json} Success-Response:
  1768. * HTTP/1.1 200 OK
  1769. * {
  1770. * "status": true,
  1771. * "status_code": 0,
  1772. * "message": "",
  1773. * "data":[
  1774. * "video":{
  1775. * 'url':22222,
  1776. * 'name':22222,
  1777. * 'type':22222,
  1778. * 'agent_id':22222,
  1779. * }
  1780. * "banner":{
  1781. * 'url':22222,
  1782. * 'name':22222,
  1783. * 'type':22222,
  1784. * 'agent_id':22222,
  1785. * },
  1786. * ]
  1787. * }
  1788. * @apiErrorExample {json} Error-Response:
  1789. * HTTP/1.1 400 Bad Request
  1790. * {
  1791. * "state": false,
  1792. * "code": 1000,
  1793. * "message": "传入参数不正确",
  1794. * "data": null or []
  1795. * }
  1796. * 可能出现的错误代码:
  1797. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1798. */
  1799. public function albumGetBanner(Request $request)
  1800. {
  1801. $userAuth = Auth('api')->user();
  1802. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1803. if($userAuth->up_agent_id == 0){
  1804. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1805. $agent_id = $user_agent['id'];
  1806. }else{
  1807. $agent_id = $userAuth->up_agent_id;
  1808. }
  1809. $banner = AgentBannerModel::where([['agent_id',$agent_id],['type',0]])->get();
  1810. $video = AgentBannerModel::where([['agent_id',$agent_id],['type',1]])->first();
  1811. return $this->api(compact('banner','video'));
  1812. }
  1813. /**
  1814. * @api {get} /api/album/get-customer 获取客户信息(get-customer)
  1815. * @apiDescription 获取客户信息(get-customer)
  1816. * @apiGroup Album
  1817. * @apiPermission none
  1818. * @apiVersion 0.1.0
  1819. * @apiParam {int} [store_id] 商户id
  1820. * @apiParam {string} [openid_id] open_id
  1821. * @apiSuccessExample {json} Success-Response:
  1822. * HTTP/1.1 200 OK
  1823. * {
  1824. * "status": true,
  1825. * "status_code": 0,
  1826. * "message": "",
  1827. * "data":[
  1828. * "purpose_level":
  1829. * "comment":
  1830. * "phone":
  1831. * "tips":
  1832. * ]
  1833. * }
  1834. * @apiErrorExample {json} Error-Response:
  1835. * HTTP/1.1 400 Bad Request
  1836. * {
  1837. * "state": false,
  1838. * "code": 1000,
  1839. * "message": "传入参数不正确",
  1840. * "data": null or []
  1841. * }
  1842. * 可能出现的错误代码:
  1843. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1844. */
  1845. public function albumGetCustomer(Request $request)
  1846. {
  1847. $userAuth = Auth('api')->user();
  1848. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1849. $validator = Validator::make($request->all(), [
  1850. 'store_id' => 'required',
  1851. 'open_id'=>'required'
  1852. ],[
  1853. 'store_id.required'=>'缺少商户参数',
  1854. 'open_id.required'=>'缺少用户参数',
  1855. ]);
  1856. if ($validator->fails()) {
  1857. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1858. }
  1859. $store_id = $request->input('store_id');
  1860. $open_id = $request->input('open_id');
  1861. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  1862. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1863. $customer = CustomerDetailsModel::where([['store_id',$store_id],['open_id',$open_id],['agent_id',$user_agent->id]])->first(['purpose_level','comment','tips']);
  1864. if(!empty($customer)){
  1865. $user = AlbumUserModel::where([['store_id',$store_id],['wechat_open_id',$open_id]])->first();
  1866. $customer->phone = $user->phone;
  1867. }
  1868. return $this->api(compact('customer'));
  1869. }
  1870. /**
  1871. * @api {post} /api/album/set-customer 设置客户信息(set-customer)
  1872. * @apiDescription 设置客户信息(set-customer)
  1873. * @apiGroup Album
  1874. * @apiPermission none
  1875. * @apiVersion 0.1.0
  1876. * @apiParam {int} [store_id] 商户id
  1877. * @apiParam {string} [openid_id] openid
  1878. * @apiParam {int} [level] 意向等级 1 25% 2 50% 3 75% 4 100%
  1879. * @apiParam {string} [comment] 备注
  1880. * @apiParam {string} [tips] 标签
  1881. * @apiSuccessExample {json} Success-Response:
  1882. * HTTP/1.1 200 OK
  1883. * {
  1884. * "status": true,
  1885. * "status_code": 0,
  1886. * "message": "",
  1887. * "data":[
  1888. * "purpose_level":
  1889. * "comment":
  1890. * "phone":
  1891. * "tips":
  1892. * ]
  1893. * }
  1894. * @apiErrorExample {json} Error-Response:
  1895. * HTTP/1.1 400 Bad Request
  1896. * {
  1897. * "state": false,
  1898. * "code": 1000,
  1899. * "message": "传入参数不正确",
  1900. * "data": null or []
  1901. * }
  1902. * 可能出现的错误代码:
  1903. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1904. */
  1905. public function albumSetCustomer(Request $request)
  1906. {
  1907. $userAuth = Auth('api')->user();
  1908. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1909. $validator = Validator::make($request->all(), [
  1910. 'store_id' => 'required',
  1911. 'purpose_level' => 'required',
  1912. 'comment' => 'required',
  1913. 'tips' => 'required',
  1914. 'open_id'=>'required'
  1915. ],[
  1916. 'store_id.required'=>'缺少商户参数',
  1917. 'purpose_level.required'=>'缺少意向等级参数',
  1918. 'comment.required'=>'缺少客户备注参数',
  1919. 'tips.required'=>'缺少标签参数',
  1920. 'open_id.required'=>'缺少用户参数',
  1921. ]);
  1922. if ($validator->fails()) {
  1923. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1924. }
  1925. $store_id = $request->input('store_id');
  1926. $open_id = $request->input('open_id');
  1927. $add = $request->input();
  1928. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  1929. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1930. $customer = CustomerDetailsModel::where([['store_id',$store_id],['open_id',$open_id],['agent_id',$user_agent->id]])->first(['purpose_level','comment','tips']);
  1931. $add['agent_id'] = $user_agent->id;
  1932. $user = CustomerDetailsModel::create($add);
  1933. $customer->phone = $user->phone;
  1934. return $this->api([],0,'success');
  1935. }
  1936. /**
  1937. * @api {get} /api/album/customer-goods 获取客户浏览商品统计(customer-goods)
  1938. * @apiDescription 获取客户浏览商品统计(customer-goods)
  1939. * @apiGroup Album
  1940. * @apiPermission none
  1941. * @apiVersion 0.1.0
  1942. * @apiSuccessExample {json} Success-Response:
  1943. * HTTP/1.1 200 OK
  1944. * {
  1945. * "status": true,
  1946. * "status_code": 0,
  1947. * "message": "",
  1948. * "data":[
  1949. * {
  1950. * "name":
  1951. * "count":
  1952. * },
  1953. * ]
  1954. * }
  1955. * @apiErrorExample {json} Error-Response:
  1956. * HTTP/1.1 400 Bad Request
  1957. * {
  1958. * "state": false,
  1959. * "code": 1000,
  1960. * "message": "传入参数不正确",
  1961. * "data": null or []
  1962. * }
  1963. * 可能出现的错误代码:
  1964. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1965. */
  1966. public function albumCustomerGoods(Request $request)
  1967. {
  1968. $userAuth = Auth('api')->user();
  1969. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1970. $validator = Validator::make($request->all(), [
  1971. 'store_id' => 'required',
  1972. 'open_id'=>'required'
  1973. ],[
  1974. 'store_id.required'=>'缺少商户参数',
  1975. 'open_id.required'=>'缺少用户参数',
  1976. ]);
  1977. if ($validator->fails()) {
  1978. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1979. }
  1980. $store_id = $request->input('store_id');
  1981. $open_id = $request->input('open_id');
  1982. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  1983. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1984. if($user_agent) $res= AlbumWatchRecord::where([['action',3],['store_id',$store_id],['open_id',$open_id]])->get();
  1985. $arr = array();
  1986. foreach ($res as $key=>$val){
  1987. $detail = json_decode($val['detail'],true);
  1988. //dd($detail);die;
  1989. $goods_id = $detail['goods_id'];
  1990. $goods = AlbumProductModel::where([['id',$goods_id]])->first();
  1991. if($goods){
  1992. $count= AlbumWatchRecord::where([['action',3],['store_id',$store_id],['open_id',$open_id],['detail',$detail]])->count();
  1993. $arr[] = ['name'=>$goods->name,'count'=>$count];
  1994. }
  1995. }
  1996. return $this->api($arr,0,'success');
  1997. }
  1998. }