1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: zhangjun
5+ * Date: 05/09/2018
6+ * Time: 2:27 PM
7+ */
8+
9+ class MiniProgram_Gif_IndexController extends MiniProgramController
10+ {
11+
12+ private $ gifMiniProgramId = 104 ;
13+ private $ action = "duckChat.message.send " ;
14+ private $ groupType = "g " ;
15+ private $ u2Type = "u " ;
16+ private $ userRelationAction = "duckChat.user.relation " ;
17+ private $ limit =30 ;
18+
19+ public function getMiniProgramId ()
20+ {
21+ return $ this ->gifMiniProgramId ;
22+ }
23+
24+ public function requestException ($ ex )
25+ {
26+ $ this ->showPermissionPage ();
27+ }
28+
29+ public function preRequest ()
30+ {
31+ }
32+
33+ public function doRequest ()
34+ {
35+ header ('Access-Control-Allow-Origin: * ' );
36+ $ method = $ _SERVER ['REQUEST_METHOD ' ];
37+ $ tag = __CLASS__ ."- " .__FUNCTION__ ;
38+ if ($ method == 'POST ' ) {
39+ try {
40+ $ type = isset ($ _POST ['type ' ]) ? $ _POST ['type ' ] :"send_msg " ;
41+ switch ($ type ) {
42+ case "send_msg " :
43+ $ this ->sendWebMessage ($ _POST );
44+ break ;
45+ case "add_gif " :
46+ $ this ->addGif ($ _POST );
47+ }
48+ $ this ->ctx ->Wpf_Logger ->error ($ tag , "post msg = " . json_encode ($ _POST ));
49+ echo json_encode (["errorCode " => "success " , "errorInfo " => "" ]);
50+ }catch (Exception $ ex ) {
51+ echo json_encode (["errorCode " => "error.alert " , 'errorInfo ' => $ ex ->getMessage ()]);
52+ }
53+ } else {
54+ $ pageUrl = $ _COOKIE ['duckchat_page_url ' ];
55+ $ pageUrl = parse_url ($ pageUrl );
56+ parse_str ($ pageUrl ['query ' ], $ queries );
57+ $ x = $ queries ['x ' ];
58+ list ($ type , $ toId ) = explode ("- " , $ x );
59+ if ($ toId == $ this ->userId ) {
60+ return ;
61+ }
62+
63+ if ($ type == $ this ->groupType ) {
64+ $ roomType = "MessageRoomGroup " ;
65+ }elseif ($ type == $ this ->u2Type ) {
66+ $ roomType = "MessageRoomU2 " ;
67+ }
68+
69+ $ results = [
70+ "roomType " => $ roomType ,
71+ "toId " => $ toId ,
72+ "fromUserId " => $ this ->userId ,
73+ ];
74+
75+ $ gifs = [
76+ [
77+ "url " => "https://media.giphy.com/media/NWLUwtaivTes8/200w_d.gif " ,
78+ "width " => 200 ,
79+ "height " => 200
80+ ],
81+ [
82+ "url " => "https://media.giphy.com/media/NWLUwtaivTes8/200w_d.gif " ,
83+ "width " => 200 ,
84+ "height " => 200
85+ ],
86+ [
87+ "url " => "https://media.giphy.com/media/NWLUwtaivTes8/200w_d.gif " ,
88+ "width " => 200 ,
89+ "height " => 200
90+ ],
91+ [
92+ "url " => "https://media.giphy.com/media/NWLUwtaivTes8/200w_d.gif " ,
93+ "width " => 200 ,
94+ "height " => 200
95+ ],
96+ [
97+ "url " => "https://media.giphy.com/media/NWLUwtaivTes8/200w_d.gif " ,
98+ "width " => 200 ,
99+ "height " => 200
100+ ],[
101+ "url " => "https://media.giphy.com/media/NWLUwtaivTes8/200w_d.gif " ,
102+ "width " => 200 ,
103+ "height " => 200
104+ ]
105+ ];
106+ $ result = $ this ->ctx ->SiteUserGifTable ->getGif ($ this ->userId , 0 , $ this ->limit );
107+ $ gifs = array_merge_recursive ($ result , $ gifs );
108+ $ results ['gifs ' ] = $ gifs ;
109+ $ results ['gifs ' ] = json_encode ($ results ['gifs ' ]);
110+ echo $ this ->display ("miniProgram_gif_index " , $ results );
111+ return ;
112+ }
113+ }
114+ private function sendWebMessage ($ msg )
115+ {
116+
117+ $ sendMsg = $ msg ['message ' ];
118+ $ roomType = $ sendMsg ['roomType ' ] == "MessageRoomU2 " ? \Zaly \Proto \Core \MessageRoomType::MessageRoomU2 : \Zaly \Proto \Core \MessageRoomType::MessageRoomGroup;
119+
120+ if ($ roomType == \Zaly \Proto \Core \MessageRoomType::MessageRoomU2) {
121+ $ userRelationReq = new \Zaly \Proto \Plugin \DuckChatUserRelationRequest ();
122+ $ userRelationReq ->setUserId ($ this ->userId );
123+ $ userRelationReq ->setOppositeUserId ($ sendMsg ['toUserId ' ]);
124+ $ response = $ this ->requestDuckChatInnerApi ($ this ->gifMiniProgramId , $ this ->userRelationAction , $ userRelationReq );
125+
126+ if ($ response ->getRelationType () != \Zaly \Proto \Core \FriendRelationType::FriendRelationFollow) {
127+ $ errorCode = $ this ->zalyError ->errorFriend ;
128+ $ errorInfo = $ this ->zalyError ->getErrorInfo ($ errorCode );
129+ throw new Exception ($ errorInfo );
130+ }
131+
132+ $ userRelationReq = new \Zaly \Proto \Plugin \DuckChatUserRelationRequest ();
133+ $ userRelationReq ->setUserId ($ sendMsg ['toUserId ' ]);
134+ $ userRelationReq ->setOppositeUserId ($ this ->userId );
135+ $ response = $ this ->requestDuckChatInnerApi ($ this ->gifMiniProgramId , $ this ->userRelationAction , $ userRelationReq );
136+
137+ if ($ response ->getRelationType () != \Zaly \Proto \Core \FriendRelationType::FriendRelationFollow) {
138+ $ errorCode = $ this ->zalyError ->errorFriend ;
139+ $ errorInfo = $ this ->zalyError ->getErrorInfo ($ errorCode );
140+ throw new Exception ($ errorInfo );
141+ }
142+
143+ }
144+
145+ $ webMsg = new \Zaly \Proto \Core \WebMessage ();
146+
147+ $ webMsg ->setWidth ($ sendMsg ['web ' ]['width ' ]);
148+ $ webMsg ->setHeight ($ sendMsg ['web ' ]['height ' ]);
149+ $ webMsg ->setCode ($ sendMsg ['web ' ]['code ' ]);
150+ $ webMsg ->setHrefURL ($ sendMsg ['web ' ]['hrefURL ' ]);
151+
152+ $ message = new \Zaly \Proto \Core \Message ();
153+ $ message ->setMsgId ($ sendMsg ['msgId ' ]);
154+ $ message ->setType (\Zaly \Proto \Core \MessageType::MessageWeb);
155+ $ message ->setTimeServer ($ sendMsg ['timeServer ' ]);
156+ $ message ->setWeb ($ webMsg );
157+ $ message ->setRoomType ($ roomType );
158+ $ message ->setFromUserId ($ this ->userId );
159+ if ($ roomType == \Zaly \Proto \Core \MessageRoomType::MessageRoomU2) {
160+ $ message ->setToUserId ($ sendMsg ['toUserId ' ]);
161+ } else {
162+ $ message ->setToGroupId ($ sendMsg ['toGroupId ' ]);
163+ }
164+
165+ $ duckchatReqData = new \Zaly \Proto \Plugin \DuckChatMessageSendRequest ();
166+ $ duckchatReqData ->setMessage ($ message );
167+ $ this ->requestDuckChatInnerApi ($ this ->gifMiniProgramId , $ this ->action , $ duckchatReqData );
168+ }
169+
170+ public function addGif ($ data )
171+ {
172+ $ gifId = $ data ['gifId ' ];
173+ $ data = [
174+ 'userId ' => $ this ->userId ,
175+ 'gifId ' => $ gifId ,
176+ 'addTime ' => ZalyHelper::getMsectime ()
177+ ];
178+ $ this ->ctx ->SiteUserGifTable ->addGif ($ data );
179+ }
180+ }
0 commit comments